英文:
Spring Bind @PathVariable to JavaBean
问题
我尝试将一个对象绑定到Spring控制器,以便它可以用作@PathVariable
。我想这样做,因为有一些@PathVariable
我想要传递。我已经尝试了来自 https://stackoverflow.com/questions/17149425/bind-path-variables-to-a-custom-model-object-in-spring 和 https://stackoverflow.com/questions/55792555/is-it-possible-to-bind-path-variable-and-request-param-into-a-single-object 的解决方案。但都不起作用。
我在我的控制器中创建了类似这样的内容。
@RestController
@RequestMapping("/buildings")
@RequiredArgsConstructor
public class BuildingController {
private final BuildingService buildingService;
@GetMapping("/{buildingId}/floors/{floorId}/rooms/{roomId}/sections")
public Flux<SectionDTO> getRoomSections(BuildingRequestBean request) {
return this.buildingService.getRoomSections(request);
}
}
以及 BuildingRequestBean.java
类似这样
@Getter
@Setter
public class BuildingRequestBean {
private String buildingId;
private String floorId;
private String roomId;
}
当我检查BuildingRequestBean
时,当我使用GET localhost:8080/buildings/a/floors/b/rooms/c/sections
调用它时,属性是null。
然而,如果我将其作为@RequestParam
调用,类似于这样 GET localhost:8080/buildings/{buildingId}/floors/{floorId}/rooms/{roomId}/sections?buildingId=a&floorId=b&roomId=c
,它将不会是null。
如何修复它,使其表现得像@PathVariable
而不是像@RequestParam
?
英文:
I try to bind an object in Spring controller so it can be used as @PathVariable
. I want to do so, since there are some @PathVariable
that I want to pass. I have tried the solution from https://stackoverflow.com/questions/17149425/bind-path-variables-to-a-custom-model-object-in-spring and also https://stackoverflow.com/questions/55792555/is-it-possible-to-bind-path-variable-and-request-param-into-a-single-object. But both are not working.
I have created something like this in my controller.
@RestController
@RequestMapping("/buildings")
@RequiredArgsConstructor
public class BuildingController {
private final BuildingService buildingService;
@GetMapping("/{buildingId}/floors/{floorId}/rooms/{roomId}/sections")
public Flux<SectionDTO> getRoomSections(BuildingRequestBean request) {
return this.buildingService.getRoomSections(request);
}
}
and BuildingRequestBean.java
like this
@Getter
@Setter
public class BuildingRequestBean {
private String buildingId;
private String floorId;
private String roomId;
}
When I check BuildingRequestBean
, the attributes is null when I call it with GET localhost:8080/buildings/a/floors/b/rooms/c/sections
.
However, it will not null if I call it as @RequestParam
, something like this GET localhost:8080/buildings/{buildingId}/floors/{floorId}/rooms/{roomId}/sections?buildingId=a&floorId=b&roomId=c
How to fix it so it will behave like @PathVariable
rather than behave like @RequestParam
?
答案1
得分: 0
你可以通过使用@ModelAttribute
来获取它。
尝试使用以下代码:
@GetMapping("/{buildingId}/floors/{floorId}/rooms/{roomId}/sections")
public Flux<SectionDTO> getRoomSections(@ModelAttribute BuildingRequestBean request) {
return this.buildingService.getRoomSections(request);
}
英文:
U can get it by using @ModelAttribute
Try with this:
@GetMapping("/{buildingId}/floors/{floorId}/rooms/{roomId}/sections")
public Flux<SectionDTO> getRoomSections(@ModelAttribute BuildingRequestBean request) {
return this.buildingService.getRoomSections(request);
}
答案2
得分: -1
PathVariable 必须添加到函数参数中
尝试这样做:
@GetMapping("/{buildingId}/floors/{floorId}/rooms/{roomId}/sections")
public Flux<SectionDTO> getRoomSections(@PathVariable String buildingId, @PathVariable String floorId, @PathVariable String roomId) {
英文:
PathVariable must be added to the function parameter
Try this :
@GetMapping("/{buildingId}/floors/{floorId}/rooms/{roomId}/sections")
public Flux<SectionDTO> getRoomSections(@PathVariable String buildingId,@PathVariable String floorId ,@PathVariable String roomId) {
专注分享java语言的经验与见解,让所有开发者获益!
评论