I'm trying to write a simple HTTP REST service using Spring 4.
I'm having troubles sending data to a POST endpoint
@RequestMapping(value = "/onlyPost", produces = "application/json", method
= RequestMethod.POST)
public @ResponseBody ResponseEntity<?> createUser(@RequestParam("value1")
String param1, @RequestParam("value2") String param2) {
....
}
"message": "Required String parameter 'value1' is not present",
@RequestParam
is used to read a URL query parameter.
http://localhost:8080/springmvc/onlyPost?value1=foo&value2=bar
For instance, in the URL above, value1
and value2
are query parameters that you can read using that annotation.
But if you want to read a JSON request instead, you need to change the method to:
@RequestMapping(value = "/onlyPost", method = RequestMethod.POST,
consumes = "application/json")
public @ResponseBody ResponseEntity<?> createUser(@RequestBody User user) {
....
}
where User is a POJO holding the two fields:
public class User {
private String value1;
private String value2;
// getters and setters...
}