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.