Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST @RequestBody is always empty

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

like image 425
Mr.wiseguy Avatar asked Dec 04 '15 08:12

Mr.wiseguy


2 Answers

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.

like image 135
Driss Amri Avatar answered Oct 11 '22 08:10

Driss Amri


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.

like image 33
Tom Sebastian Avatar answered Oct 11 '22 09:10

Tom Sebastian