I'm using Spring Boot (latest version, 1.3.6) and I want to create a REST endpoint which accepts a bunch of arguments and a JSON object. Something like:
curl -X POST http://localhost:8080/endpoint \
-d arg1=hello \
-d arg2=world \
-d json='{"name":"john", "lastNane":"doe"}'
In the Spring controller I'm currently doing:
public SomeResponseObject endpoint(
@RequestParam(value="arg1", required=true) String arg1,
@RequestParam(value="arg2", required=true) String arg2,
@RequestParam(value="json", required=true) Person person) {
...
}
The json
argument doesn't get serialized into a Person object.
I get a
400 error: the parameter json is not present.
Obviously, I can make the json
argument as String and parse the payload inside the controller method, but that kind of defies the point of using Spring MVC.
It all works if I use @RequestBody
, but then I loose the possibility to POST separate arguments outside the JSON body.
Is there a way in Spring MVC to "mix" normal POST arguments and JSON objects?
Yes,is possible to send both params and body with a post method: Example server side:
@RequestMapping(value ="test", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Person updatePerson(@RequestParam("arg1") String arg1,
@RequestParam("arg2") String arg2,
@RequestBody Person input) throws IOException {
System.out.println(arg1);
System.out.println(arg2);
input.setName("NewName");
return input;
}
and on your client:
curl -H "Content-Type:application/json; charset=utf-8"
-X POST
'http://localhost:8080/smartface/api/email/test?arg1=ffdfa&arg2=test2'
-d '{"name":"me","lastName":"me last"}'
Enjoy