9

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

Community
  • 1
  • 1
Mr.wiseguy
  • 4,092
  • 10
  • 35
  • 67
  • I don't think it has anything to do with the problem, however with PostMan you are sending `"lattitude"` instead of `"latitude"` and `"timestamp"` instead of `"lastActive"`. – francesco foresti Dec 04 '15 at 08:58
  • @francescoforesti, thanks for the headsup. I thought I had changed that. I'l change it in the post. (code was already up to date) – Mr.wiseguy Dec 04 '15 at 09:03
  • @Mr.wiseguy your problems got solved ? – Tom Sebastian Dec 04 '15 at 10:01
  • @TomSebastian, Yes. In this case Driss Amri and you both gave the solution. The total solution can be found in http://stackoverflow.com/questions/34067101/ajax-request-to-spring-rest-api-415-error/ – Mr.wiseguy Dec 04 '15 at 10:38
  • This may be not an answer but I think it may help if you are using Lombok with IntelliJ and have this problem. For me it happen because of I'm using Lombok with @Data annotation, means getters and setters are supposed to be generated implicitly, but they were not. Lombok didn't do it job properly. I tried to add getters/setters method explicitly and it works. – Andiana Oct 09 '19 at 14:05

2 Answers2

10

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.

Driss Amri
  • 1,805
  • 2
  • 20
  • 28
  • 1
    '@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. – Mr.wiseguy Dec 04 '15 at 09:02
  • As far as I know, @RequestBody is not optional. I think you are confusing it with @ResponseBody? https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html – Driss Amri Dec 04 '15 at 09:15
  • Adding @RequestBody brings me back to my original question. This brings up a 415 media not supported error. – Mr.wiseguy Dec 04 '15 at 09:20
  • Are you sending the "Content-Type: application/json" header with postman? – Driss Amri Dec 04 '15 at 09:24
  • http://stackoverflow.com/questions/34067101/ajax-request-to-spring-rest-api-415-error for my 415 error – Mr.wiseguy Dec 04 '15 at 09:26
  • 3
    Every @RequestBody bean must implement Serilizable interface. – Vijay Shegokar Apr 27 '17 at 15:00
5

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.

Tom Sebastian
  • 3,373
  • 5
  • 29
  • 54