有什么办法可以将这两个值封装到一个对象中吗?
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 类具有这三个属性(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.
}
然后在你的 Controller 中:
public ResponseEntity<TestResponse> test(@Valid MyRequest request) {
...
}
关于java - @RequestParam 和@PathVariable 封装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170915/