简体   繁体   中英

Request body is coming as Null at REST api endpoint

I am writing a REST API that has a PUT operation, using which I want to save the data to MongoDB.

My PUT operation accepts data in JSON format which would get inserted to MongoDB in the same format itself. Problem I am facing is that when I provide a JSON object in the request body, I receive it as NULL on the server side(REST API endpoint)

Below is my code in the controller

@RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    Environment_per_user SaveData(Environment_per_user Env_Details) {
        return service.SaveEnvDetails(Env_Details);
    }

I am using Mozilla Firefox's REST Client to test test my Web-service.

Debugger at my REST Api's endpoint that shows that the request body is coming as NULL

I am sure I am missing something elementary but somehow I am not able to figure it out.

Answering my own question, If it helps someone tomorrow.

This is what I did. Added @RequestBody annotation in my controller method. Which would tell that the JSON object is part of request body.

@RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    Environment_per_user SaveData(@RequestBody Environment_per_user Env_Details) {
        return service.SaveEnvDetails(Env_Details);
    }

And in the request-header in my REST client -> Added header, Content-Type = 'application/json'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM