I'm doing a post request in SpringBoot and testing it with postman, but when I pass the body in Postman and try to read it in my application, it throws the error.
This is the method in Spring:

@PostMapping(path=PathConstants.START_ACTION)
    public String start(@PathVariable String processDefinitionId, @RequestBody(required=false) String params){

if(params!=null) {
                Gson gson = new Gson();
                Map<String,Object> pvar = gson.fromJson(params, Map.class);
                System.out.println(pvar);           
            } 
}

In Postman I pass params this way:

enter image description here

I specified in the header the content-type as application/json.
But then, if I pass my params using "Param" tab

enter image description here

it works. But I need to pass them as body and not params. Where is the problem here?

1

There are 1 best solutions below

0
On BEST ANSWER

Method 1 :

Header Content-Type is application-json. So Spring try to construct your json into a LinkedHashMap.

Now, try this...

@PostMapping(path=PathConstants.START_ACTION)
    public String start(@PathVariable String processDefinitionId, @RequestBody(required=false) Map<String, Object> bodyObject){

if(MapUtils.isNotEmpty(bodyObject)) {
              Map<String,Object> pvar = bodyObject;
}

instead of

@PostMapping(path=PathConstants.START_ACTION)
    public String start(@PathVariable String processDefinitionId, @RequestBody(required=false) String params){

if(params!=null) {
                Gson gson = new Gson();
                Map<String,Object> pvar = gson.fromJson(params, Map.class);
                System.out.println(pvar);           
            } 
}

and pass header the content-type as application/json. this will work..

Method 2 ::

Now, if you still want to use your own old signature method like this ..

 @PostMapping(path=PathConstants.START_ACTION)
        public String start(@PathVariable String processDefinitionId, @RequestBody(required=false) String params){}

then in header the content-type as text/plain. then your older method will work also..