Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: How to get parameters from POST body?

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.

like image 682
Harinder Avatar asked Mar 04 '14 05:03

Harinder


People also ask

How do you get the RequestBody value?

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.

What is the difference between @RequestBody and @ResponseBody?

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.

What is RequestBody annotation in Spring boot?

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.

Does Spring @RequestBody support the GET method?

Thanks. spring v5. 3.6 supports request body with GET.


4 Answers

You can get param from request.

@ResponseBody
public ResponseEntity<Boolean> saveData(HttpServletRequest request,
            HttpServletResponse response, Model model){
   String jsonString = request.getParameter("json");
}
like image 187
Jason Avatar answered Oct 06 '22 22:10

Jason


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..

like image 31
mbarish-me Avatar answered Oct 06 '22 22:10

mbarish-me


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>
like image 45
redzedi Avatar answered Oct 06 '22 22:10

redzedi


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

like image 31
LambdaExpression Avatar answered Oct 07 '22 00:10

LambdaExpression