I have a simple controller method with more than 7 parameters and would like to refactor it using a model object instead, i.e. to extract a parameter object:
@RequestMapping(value="/{one}")
public String controllerMethod(@PathVariable(value="one") String one, @RequestParam String two, ... @RequestBody body) {
...
}
I tried to extract an object with setters and getters and pathVariable and requestParameters are mapped by name. However I have troubles making the same for @RequestBody, it doesn't work for me even if I put @RequestBody into 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
}
- How to keep @RequestBody parameter in extracted POJO?
- Another question is how to control name of parameters, i.e. if
parameter name differs from the field name in POJO, is there any
annotation? This one doesn't work:
public void setOne(@RequestParameter(value="o") String one) {this.one = one}
- How to mark the fields as required or give a default value like in the @RequestParameter annotation?