Spring: How to get parameters from POST body?
You can get param from request.
@ResponseBody
public ResponseEntity<Boolean> saveData(HttpServletRequest request,
HttpServletResponse response, Model model){
String jsonString = request.getParameter("json");
}
You can bind the json to a POJO using MappingJacksonHttpMessageConverter
. Thus your controller signature can read :-
public ResponseEntity<Boolean> saveData(@RequestBody RequestDTO req)
Where RequestDTO needs to be a bean appropriately annotated to work with jackson serializing/deserializing. Your *-servlet.xml file should have the Jackson message converter registered in RequestMappingHandler as follows :-
<list >
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
You can get entire post body into a POJO. Following is something similar
@RequestMapping(
value = { "/api/pojo/edit" },
method = RequestMethod.POST,
produces = "application/json",
consumes = ["application/json"])
@ResponseBody
public Boolean editWinner( @RequestBody Pojo pojo) {
Where each field in Pojo (Including getter/setters) should match the Json request object that the controller receives..
You can try using @RequestBodyParam
@RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Boolean> saveData(@RequestBodyParam String source,@RequestBodyParam JsonDto json) throws MyException {
...
}
https://github.com/LambdaExpression/RequestBodyParam