Spring REST @RequestBody is always empty

22,411

Solution 1

I don't see a @RequestBody in your Controller for the UserLocation object? Also make sure your properties have getters and setters.

public UserLocation updateUserLocation(@PathVariable("id") int id, UserLocation user) {        

When doing a HTTP PUT, you WILL have to put extra logic to persist your object to the database. You will need to call your DAO or Repository to persist your object. Usually you map your incoming UserLocation object to a real JPA/Hibernate entity that you persist. This will not happen automatically.

Solution 2

Problem is you missed to annotate the UserLocation parameter with @RequestBody

..updateUserLocation(@PathVariable("id") int id, @RequestBody UserLocation user)

Also make sure to generate getters and setters for UserLocation memeber variables.

Share:
22,411
Mr.wiseguy
Author by

Mr.wiseguy

Updated on August 08, 2022

Comments

  • Mr.wiseguy
    Mr.wiseguy almost 2 years

    So after I fixed the '415 media not supported' error (415 media not supported), I encountered a new issue. My RequestBody is always empty. When I send a request via Chrome PostMan I always get a serialized entity back with only null values. I am using Spring (4.2.3.RELEASE),Jackson-databind (2.6.3) and jackson-core (2.6.3) in my project. I am using annotation based configuration in my project (@EnableWebMvc to make spring automatically discover HTTPMessageConverters).


    Other posts

    I am aware of other posts on stackoverflow, with almost the same problem. Yet they did not provide me with an answer. Also the most of the posts are for older Spring versions (pre 4.0), so some things are quite different now.

    Similar posts:

    @RepsonseBody is always empty in Spring

    Spring's @RequestBody providing empty string on POST

    Using HttpServletRequest request


    Spring controller

    In my Spring @RestController I have the following code:

    @RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
    public UserLocation updateUserLocation(@PathVariable("id") int id, UserLocation user) {        
            return user;        
    }
    

    I am using a separate model class (UserLocation) for my data binding. This is because this way I have more control of the data my API sends and recieves.


    Databinding class (UserLocation)

    The UserLocation class consits of 3 properties with a constructor and the required getters and setters. (I could make those properties public, but I first want to fix this problem).

    public class UserLocation {
    
        private Float latitude;
    
        private Float longitude;
    
        private Date lastActive;
    }
    

    JSON body

    Via my AngularJS Ajax call ($http.PUT) I make a call to the spring controller with the following data:

     {
        "latitude": 52.899370,
        "longitude": 5.804548,
        "lastActive": 1449052628407
     }
    

    PostMan request

    I am developing a Cordova application, so to test requests without the need of building my application to my mobile phone, I am using Chrome PostMan Plugin.

    I am making the following call:

    URL: http://server:port/app/location/update/1

    Method: PUT

    Headers: Content-Type: application/json

    Body:

    {
        "latitude": 52.899370,
        "longitude": 5.804548,
        "timestamp": 1449052628407
    }
    

    Request result

    With the request I get the following result:

    {"latitude":null,"longitude":null,"lastActive":null}
    

    This means that Spring does make a new instance of my UserLocation class, but it does not fill it with the data from the body..


    Spring PUT method

    When using the PUT method in a Spring controller, isn't the entity updated immediately? So that would mean that there would be no extra logic in the controller for updating the entity right? (if the entity is of course a Hibernate/JPA model, which can be updated).


    I cannot seem to figure out the problem. Anyone knows what I am doing wrong?


    Update

    Adding @RequestBody to my controller code:

    @RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
    public UserLocation updateUserLocation(@PathVariable("id") int id, @RequestBody UserLocation user) {        
                return user;        
    }
    

    Brings me back to my original question (415 media not supported). Adding this throws a 415 Media not supported error I cannot seem to fix.


    Fixed. Solution below

  • Mr.wiseguy
    Mr.wiseguy over 8 years
    '@RequestBody' is not nessecairy anymore since the introduction of '@RestController' (Spring 4.0). I am returning the instance of UserLocation Spring makes for me. That results in an empty JSON string (see my post). Thanks for the info about persisting. Then I will still need to implement that code.
  • Driss Amri
    Driss Amri over 8 years
    As far as I know, @RequestBody is not optional. I think you are confusing it with @ResponseBody? docs.spring.io/spring/docs/current/javadoc-api/org/…
  • Mr.wiseguy
    Mr.wiseguy over 8 years
    Adding @RequestBody brings me back to my original question. This brings up a 415 media not supported error.
  • Driss Amri
    Driss Amri over 8 years
    Are you sending the "Content-Type: application/json" header with postman?
  • Mr.wiseguy
    Mr.wiseguy over 8 years
  • Vijay Shegokar
    Vijay Shegokar about 7 years
    Every @RequestBody bean must implement Serilizable interface.