Spring绑定`@PathVariable`到JavaBean

huangapple 未分类评论46阅读模式
英文:

Spring Bind @PathVariable to JavaBean

问题

我尝试将一个对象绑定到Spring控制器,以便它可以用作@PathVariable。我想这样做,因为有一些@PathVariable我想要传递。我已经尝试了来自 https://stackoverflow.com/questions/17149425/bind-path-variables-to-a-custom-model-object-in-springhttps://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(&quot;/buildings&quot;)
@RequiredArgsConstructor
public class BuildingController {

    private final BuildingService buildingService;

    @GetMapping(&quot;/{buildingId}/floors/{floorId}/rooms/{roomId}/sections&quot;)
    public Flux&lt;SectionDTO&gt; 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&amp;floorId=b&amp;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(&quot;/{buildingId}/floors/{floorId}/rooms/{roomId}/sections&quot;)
    public Flux&lt;SectionDTO&gt; 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(&quot;/{buildingId}/floors/{floorId}/rooms/{roomId}/sections&quot;)
public Flux&lt;SectionDTO&gt; getRoomSections(@PathVariable String buildingId,@PathVariable String floorId ,@PathVariable String roomId) {

huangapple
  • 本文由 发表于 2020年4月6日 21:08:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/61060589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定