【发布时间】:2016-01-25 20:09:35
【问题描述】:
我有一个包含 7 个以上参数的简单控制器方法,并想改用模型对象对其进行重构,即提取参数对象:
@RequestMapping(value="/{one}")
public String controllerMethod(@PathVariable(value="one") String one, @RequestParam String two, ... @RequestBody body) {
...
}
我尝试使用 setter 和 getter 提取对象,并且 pathVariable 和 requestParameters 按名称映射。但是我在为@RequestBody 做同样的事情时遇到了麻烦,即使我将@RequestBody 放入setter 也对我不起作用......
public class Parameters {
private String one; // pathVariable
private String two; // requestParameter
private String body;// requestBody - always NULL!
// other fields definition
public setBody(@RequestBody String body) {this.body = body}
//other setters/getters
}
- 如何在提取的 POJO 中保留 @RequestBody 参数?
- 另一个问题是如何控制参数的名称,即如果
参数名和POJO中的字段名不一样,有没有
注解?这个不行:
public void setOne(@RequestParameter(value="o") String one) {this.one = one} - 如何将字段标记为必填项或像@RequestParameter 注释中一样提供默认值?
【问题讨论】:
标签: java spring spring-mvc model-view-controller mapping