简体   繁体   English

@RequestParam和@PathVariable封装

[英]@RequestParam and @PathVariable encapsulation

Is there any way to encapsulate this two values into one object? 有什么办法可以将这两个值封装到一个对象中?

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

I think that I should be able to do it with this way: 我认为我应该可以通过以下方式做到这一点:

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

where Request class has those three properties (customerId, reason, attributes). 其中Request类具有这三个属性(customerId,原因,属性)。

I'm using spring boot 1.5.9 我正在使用Spring Boot 1.5.9

You should be able to do this by defining an object that matches the request parameters, etc. 您应该能够通过定义与请求参数等匹配的对象来执行此操作。

Example (untested): 示例(未试用):

public class MyRequest {
   @NotNull
   private String customerId;

   @NotNull
   private String reason;

   @NotNull
   @NotEmpty
   private List<String> attributes;

   // getters and setters left out for brevity.
}

And then in your controller: 然后在您的控制器中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM