Spring MVC bean映射到类似于@BeanParam的HTTP GET请求参数
问题内容:
在Jersey中,可以使用@BeanParam
注释将请求参数映射到bean属性。
在Spring中,我只能找到@RequestBody
明显适用于请求正文而不适用于请求参数的内容。
有没有一种方法可以使用Spring将请求参数映射到Bean?
问题答案:
只需使用名称与您的请求参数匹配的字段创建Pojo Java Bean。
然后使用此类作为您的请求处理程序方法的参数(不带任何其他注释)
public class Example {
private String x;
private Integer y;
//Constructor without parameter needed!
public Example(){}
//Getter + Setter
}
@Controller
@RequestMapping("someUrl")
public class SomeController {
@RequestMapping
public String someHandler (Example example) {
System.out.println(example.getX());
return "redirect:someOtherUrl";
}
}