Ajax request for Spring REST api 415 error

I have a problem with my Spring Rest controller. I try to publish my client's ( PUT ) data ( angularJS ) on my server ( Spring ), but every time I try to send something I get a 415 Media not supported error.

With Maven, I added jackson-core (2.6.3) and jackson-databind (2.6.3) to my Spring API. I also use @EnableWebMvc to automatically add Jackson's message converter to Spring. In my Spring controller, I use @RestController to access Spring's REST methods.


My REST API controller:

 @RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public RippleUser updateUserLocation(@PathVariable("id") Integer id, @RequestBody RippleUser user) { return user; } 

I tried different types of consumption:

  • MediaType.APPLICATION_JSON_VALUE
  • "application/json"
  • No consumption
  • And so on

My RippleUser Model (partially)

 @Entity @Table(name = "user") @JsonRootName(value = "user") public class RippleUser implements Serializable { @NotNull @Column(name = "active", nullable = false) private boolean activeUser; @Column(name = "latitude", nullable = true) private Float lattitude; @Column(name = "longitude", nullable = true) private Float longitude; @Temporal(TemporalType.TIMESTAMP) @Column(name = "lastActive", nullable = true) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss") private Date timestamp; } 

In this model, I have all the necessary getters and setters.


My AngularJS client:

  httpService.updateInBackend = function (url, data, callback, errorCallback) { http.put(url, data) .then(function successCallback(response) { callback(response.data); }, function errorCallback(response) { errorCallback(response); }); }; 

URL: http://server:port/app/location/update/{id}

Data:

 params: { { "user": { "latitude": 52.899370, "longitude": 5.804548, "timestamp": 1449052628407 } } }; 

For this method, I also added @JsonRootName(value = "user") to my RippleUser model.

I also tried without a user attribute (also removed it from my model):

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

The AngularJS HTTP method (PUT, POST, DELETE, GET, etc.) checks what type is in the parameters and automatically sets the correct headers.


Chrome postman

Just to make sure I also tried this method in the Chrome Postman plugin

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

Method: PUT

Headers: Content-Type: application/json

Body

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

The given error:

 HTTP Status 415 The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. 

Update

I can get information in my RestController when I change my @ResponseBody from RippleUser to String:

 @RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public RippleUser updateUserLocation(@PathVariable("id") Integer id, @RequestBody String user) { return user; } 

I also tried sending an empty custom object, but this results in the same error message

The answer to my problem is below.

+4
source share
1 answer

The problem with this code is slightly different from what I expected. Error 415, not supported by the media, is thrown by Spring because it cannot fill in all the required fields with a message.

My solution was to add a class specifically used by Spring to convert data to and from when sending and receiving. The advantage of this extra class is that I can control the data that the API sends and receives.


User location

Adding the following class to my project did the trick (with getters and setters):

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

This class contains all the necessary data for my PUT request. Spring automatically fills all the fields.


controller

To do this work, I also had to change my controller. The new code is as follows:

 @RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT) public UserLocation updateUserLocation(@PathVariable("id") Integer id, @RequestBody UserLocation location) { //Find user by ID //Add location to user //Update the user return //the new user } 

Hope this helps someone

+2
source

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


All Articles