How come this code just works? I didn't specify any custom converter or annotation (like @RequestBody
or @ModelAttribute
) before argument ? Request is filled correctly from this GET call:
http://localhost:8080/WS/foo?token=C124EBD7-D9A5-4E21-9C0F-3402A1EE5E9B&lastSync=2001-01-01T00:00:00&pageNo=1
Code:
@RestController
@RequestMapping(value = "/foo")
public class FooController {
@RequestMapping(method = RequestMethod.GET)
public Result<Foo> excursions(Request request) {
// ...
}
}
Request is just POJO with getters and setters. I use it to shorten argument code because plenty methods uses those same arguments ...
public class Request {
private String token;
@DateTimeFormat(pattern = IsoDateTime.DATETIME)
private Date lastSync;
private Integer pageNo;
// getters and setters
}
This was my original method before introducing Request.
@RestController
@RequestMapping(value = "/foo")
public class FooController {
@RequestMapping(method = RequestMethod.GET)
public Result<Foo> excursions(@RequestParam String token, @RequestParam @DateTimeFormat(pattern = IsoDateTime.DATETIME) Date lastSync, @RequestParam Integer pageNo) {
// ...
}
}
Request parameters will be mapped to POJOs, as it is happening in your case, by default. Additionally, if you use @ModelAttribute, an attribute in the Model
will be created. That attribute can be then used in views, e.g. JSPs, to access the object.
@RequestBody
annotation tells that the body of the request is NOT a set of form parameters like
token=C124EBD7-D9A5-4E21-9C0F-3402A1EE5E9B&lastSync=2001-01-01T00:00:00&pageNo=1
but is in some other format, such as JSON.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With