Serban Stoenescu
Serban Stoenescu

Reputation: 3896

Java Spring MVC - Send JSON request body error

I am trying to send a JSON string as a request to my application. This is my code:

@RequestMapping(
        value = "/mylink/upload",
        method = RequestMethod.POST,
        consumes ="application/json",
        produces = "application/json")
public
@ResponseBody
List<Upload> upload(
        @RequestParam(value = "hdfsLocation") String hdfsLocation

) throws Exception {
    return S3HdfsTransfer.uploadFromHDFS(hdfsLocation);
}

I am trying to send a request with Postman. The method I use is POST, the header contains: Accept "application/json",Content-Type "application/json", the request body is the following:

{
    "hdfsLocation" : "hdfs://145.160.10.10:8020"
}

This is the response I get. If I put the parameter in the URL, it works.

{
  "httpStatus": 500,
  "appErrorId": 0,
  "message": "Required String parameter 'hdfsLocation' is not present",
  "trackingId": "8c6d45fd-2da5-47ea-a213-3d4ea5764681"
}

Any idea what I am doing wrong?

Thanks, Serban

Upvotes: 0

Views: 347

Answers (2)

aasu
aasu

Reputation: 472

Shouldn't it be @RequestBody instead of @RequestParam? Also, even after using @RequestBody, the whole of the JSON string: { "hdfsLocation" : "hdfs://145.160.10.10:8020" } will be the value of String hdfsLocation and not just the hdfs url. Hence, you'll have to JSON parse that JSON by yourself to get just the hdfs url.

Upvotes: 1

Karthik R
Karthik R

Reputation: 5786

Looks like you have confused @RequestBody with @RequestParam. Do either of following :

I guess you over looked :)

Upvotes: 1

Related Questions