Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @RequestParam和@PathVariable封装_Java_Spring_Spring Mvc_Spring Boot_Request Mapping - Fatal编程技术网

Java @RequestParam和@PathVariable封装

Java @RequestParam和@PathVariable封装,java,spring,spring-mvc,spring-boot,request-mapping,Java,Spring,Spring Mvc,Spring Boot,Request Mapping,有没有办法将这两个值封装到一个对象中 public ResponseEntity<TestResponse> test(@PathVariable("customerId") String customerId, @RequestParam(name = "reason", required = true) String reason, @RequestParam(name = "attribute", required = true) List<

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

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 test(@PathVariable(“customerId”)字符串customerId,
@RequestParam(name=“reason”,required=true)字符串原因,
@RequestParam(name=“attribute”,required=true)列表属性){
我想我应该可以这样做:

public ResponseEntity<TestResponse> test(@MaybeSomeMagicAnnotation? Request request) {
公共响应性测试(@maybesomemagicanotation?请求){
其中请求类具有这三个属性(customerId、reason和attributes)


我使用的是spring boot 1.5.9,您应该能够通过定义一个与请求参数匹配的对象来实现这一点,等等

示例(未测试):

公共类MyRequest{
@NotNull
私有字符串customerId;
@NotNull
私有字符串原因;
@NotNull
@空空如也
私有列表属性;
//干将和二传手因简洁而被遗漏。
}
然后在控制器中:

public ResponseEntity<TestResponse> test(@Valid MyRequest request) {
    ...
}
公共响应性测试(@Valid MyRequest){
...
}

您应该能够通过定义与请求参数匹配的对象等来实现这一点

示例(未测试):

公共类MyRequest{
@NotNull
私有字符串customerId;
@NotNull
私有字符串原因;
@NotNull
@空空如也
私有列表属性;
//干将和二传手因简洁而被遗漏。
}
然后在控制器中:

public ResponseEntity<TestResponse> test(@Valid MyRequest request) {
    ...
}
公共响应性测试(@Valid MyRequest){
...
}

您不需要任何东西。只需创建包含这三个字段的请求类并将其用作方法参数即可。您不需要任何东西。只需创建包含这三个字段的请求类并将其用作方法参数即可。属性上所需的param和rzej自定义名称如何(集合od属性提供属性)?@pustypawel添加了注释来说明如何做到这一点。这种情况对我来说很清楚。属性名称(在URL上,我希望在类集合“attributes”中有“attribute”另一个问题-这是使该对象不可变的任何方法吗?(当我尝试这样做时,我得到java.lang.NoSuchMethodException:packages.Request.)好的。我检查了spring源代码-没有简单的方法可以做到这一点。我被选为第一个解决方案,我只是在controller中手动创建我的复杂对象。感谢关于属性上所需的参数和rzej自定义名称(集合od属性给出属性)?@pustypawel添加了注释来说明如何做到这一点。这种情况对我来说很清楚。属性名称(在URL上,我希望在类集合“attributes”中有“attribute”另一个问题-这是使该对象不可变的任何方法吗?(当我尝试这样做时,我得到java.lang.NoSuchMethodException:packages.Request.)好的。我检查了spring源代码-没有简单的方法可以做到这一点。我被选为第一个解决方案,我只是在controller中手动创建我的复杂对象。谢谢