Spring REST @RequestBody is always empty

So, after fixing the error "415 media not supported" ( 415 media is not supported ), I ran into a new problem. My RequestBody always empty. When I submit a request via Chrome PostMan , I always get a serialized object with null values. I use Spring (4.2.3.RELEASE) , Jackson-databind (2.6.3) and jackson-core (2.6.3) in my project. I am using annotation-based customization in my project ( @EnableWebMvc so spring automatically detects HTTPMessageConverters ).


Other posts

I am aware of other stackoverflow posts, with almost the same problem. But they did not give me an answer. In addition, most posts relate to older versions of spring (prior to 4.0), so now some things are completely different.

Similar entries:

@RepsonseBody is always empty in Spring

Spring @RequestBody providing an empty string in POST

Using the 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 use a separate model class ( UserLocation ) to bind data. This is because in this way I have more control over the data that my API sends and receives.


Data Binding Class (UserLocation)

The UserLocation class contains 3 properties with a constructor and the required getters and setters. (I could make these properties publicly available, but I want to fix this problem first).

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

Json body

Through my AngularJS Ajax call ( $http.PUT ) I call 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 having to create my application on a mobile phone, I use the Chrome PostMan plugin .

I make 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 } 

Query result

With the request, I get the following result:

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

This means that spring makes a new instance of my UserLocation class, but it does not populate it with data from the body.


Spring PUT Method

When using the PUT method in the spring controller, is the object updated immediately? So, this would mean that there would be no additional logic in the controller for the correct update of the object? (if the entity, of course, is a Hibernate / JPA model that can be updated).


I can not understand the problem. Does anyone know what I'm 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; } 

Returns me to the original question ( 415 media is not supported ). Adding this parameter results in a 415 Media error that is not supported. I can not fix the error.


Fixed. Solution below

+5
source share
2 answers

I do not see @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 HTTP PUT, you will need to add additional logic to store your object in the database. You will need to call your DAO or repository to save your object. Usually you map your incoming UserLocation object to the actual JPA / Hibernate object that you are saving. This will not happen automatically.

+4
source

The problem is that you missed the annotation of the UserLocation parameter with @RequestBody

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

Also, be sure to create getters and setters for UserLocation memeber variables.

+4
source

Source: https://habr.com/ru/post/1237386/


All Articles