Web-service using spring in which I have to get the params from the body of my post request? The content of the body is like:-
source=”mysource”
&json=
{
"items": [
{
"username": "test1",
"allowed": true
},
{
"username": "test2",
"allowed": false
}
]
}
And the web-service method looks like:-
@RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Boolean> saveData(@RequestBody String a) throws MyException {
return new ResponseEntity<Boolean>(uiRequestProcessor.saveData(a),HttpStatus.OK);
}
Please let me know how do I get the params from the body? I can get the whole body in my string but I don't think that would be a valid approach. Please let me know how do I proceed further.
To retrieve the body of the POST request sent to the handler, we'll use the @RequestBody annotation, and assign its value to a String. This takes the body of the request and neatly packs it into our fullName String. We've then returned this name back, with a greeting message.
If you annotate a method with @ResponseBody, spring will try to convert its return value and write it to the http response automatically. If you annotate a methods parameter with @RequestBody, spring will try to convert the content of the incoming request body to your parameter object on the fly.
Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.
Thanks. spring v5. 3.6 supports request body with GET.
You can get param from request.
@ResponseBody
public ResponseEntity<Boolean> saveData(HttpServletRequest request,
HttpServletResponse response, Model model){
String jsonString = request.getParameter("json");
}
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 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 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
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