Spring MVC bean 映射到类似于@BeanParam 的 HTTP GET 请求参数 [英] Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam
问题描述
在 Jersey 中有 @BeanParam
注释,我可以使用它将请求参数映射到 bean 属性.
In Jersey there is @BeanParam
annotation with which I can have request parameters mapped to bean attributes.
在 Spring 中,我只能找到 @RequestBody
,它显然适用于请求正文而不是请求参数.
In Spring I can find only @RequestBody
which obviously works with request body and not request parameters.
有没有办法使用 Spring 将请求参数映射到 bean?
Is there a way to have request parameters mapped to a bean using Spring?
推荐答案
只需创建一个 Pojo Java Bean,其字段的名称与您的请求参数匹配.
Simply create a Pojo Java Bean with fields with names that match your request parameters.
然后使用此类作为您的请求处理程序方法的参数(没有任何额外的注释)
Then use this class as an argument for your request handler method (without any additional annotations)
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";
}
}
这篇关于Spring MVC bean 映射到类似于@BeanParam 的 HTTP GET 请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!