@RequestParam 和@PathVariable 封装

@RequestParam and @PathVariable encapsulation

有没有办法把这两个值封装到一个对象中?

public ResponseEntity<TestResponse> test(@PathVariable("customerId") String customerId,
        @RequestParam(name = "reason", required = true) String reason,
        @RequestParam(name = "attribute", required = true) List<String> attributes) {

我想我应该可以用这种方式来做:

public ResponseEntity<TestResponse> test(@MaybeSomeMagicAnnotation? Request request) {

其中 Request class 具有这三个属性(customerId、reason、attributes)。

我正在使用 spring boot 1.5.9

你应该可以通过定义一个与请求参数匹配的对象等来做到这一点

示例(未经测试):

public class MyRequest {
   @NotNull
   private String customerId;

   @NotNull
   private String reason;

   @NotNull
   @NotEmpty
   private List<String> attributes;

   // getters and setters left out for brevity.
}

然后在你的控制器中:

public ResponseEntity<TestResponse> test(@Valid MyRequest request) {
    ...
}