ajax - POST JSON fails with 415 Unsupported media type, Spring 3 mvc

ID : 20433

viewed : 17

Tags : ajaxjsonspringposthttp-status-code-415ajax

Top 5 Answer for ajax - POST JSON fails with 415 Unsupported media type, Spring 3 mvc

vote vote


I've had this happen before with Spring @ResponseBody and it was because there was no accept header sent with the request. Accept header can be a pain to set with jQuery, but this worked for me source

$.postJSON = function(url, data, callback) {     return jQuery.ajax({     headers: {          'Accept': 'application/json',         'Content-Type': 'application/json'      },     'type': 'POST',     'url': url,     'data': JSON.stringify(data),     'dataType': 'json',     'success': callback     }); }; 

The Content-Type header is used by @RequestBody to determine what format the data being sent from the client in the request is. The accept header is used by @ResponseBody to determine what format to sent the data back to the client in the response. That's why you need both headers.

vote vote


adding content type into the request as application/json resolved the issue

vote vote


I had a similar problem but found the issue was that I had neglected to provide a default constructor for the DTO that was annotated with @RequestBody.

vote vote


I faced a similar issue and this is how I fixed it,

The problem is due to the conversion process from JSON to Java, one need to have the right run time jackson libraries for the conversion to happen correctly.

Add the following jars (through dependency or by downloading and adding to the classpath.

<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> 

This should fix the problem.

Complete Code:

function() {   $.ajax({     type: "POST",     url: "saveUserDetails.do",     data: JSON.stringify({       name: "Gerry",       ity: "Sydney"     }),     headers: {       'Accept': 'application/json',       'Content-Type': 'application/json'     },     success: function(data) {       if (data.status == 'OK')         alert('Person has been added');       else         alert('Failed adding person: ' + data.status + ', ' + data.errorMessage); } 

and the controller signature looks like this:

@RequestMapping(value = "/saveUserDetails.do", method = RequestMethod.POST) public @ResponseBody Person addPerson( @RequestBody final  Person person) { 

Hope this helps

vote vote


I believe I ran exactly into the same issue. After countless hours of fighting with the JSON, the JavaScript and the Server, I found the culprit: In my case I had a Date object in the DTO, this Date object was converted to a String so we could show it in the view with the format: HH:mm.

When JSON information was being sent back, this Date String object had to be converted back into a full Date Object, therefore we also need a method to set it in the DTO. The big BUT is you cannot have 2 methods with the same name (Overload) in the DTO even if they have different type of parameter (String vs Date) because this will give you also the 415 Unsupported Media type error.

This was my controller method

  @RequestMapping(value = "/alarmdownload/update", produces = "application/json", method = RequestMethod.POST)   public @ResponseBody   StatusResponse update(@RequestBody AlarmDownloadDTO[] rowList) {     System.out.println("hola");     return new StatusResponse();   } 

This was my DTO example (id get/set and preAlarm get Methods are not included for code shortness):

@JsonIgnoreProperties(ignoreUnknown = true) public class AlarmDownloadDTO implements Serializable {    private static final SimpleDateFormat formatHHmm = new SimpleDateFormat("HH:mm");    private String id;   private Date preAlarm;    public void setPreAlarm(Date date) {      this.preAlarm == date;   }   public void setPreAlarm(String date) {         try {       this.preAlarm = formatHHmm.parse(date);     } catch (ParseException e) {       this.preAlarm = null;     } catch (NullPointerException e){       this.preAlarm = null;     }   } } 

To make everything work you need to remove the method with Date type parameter. This error is very frustrating. Hope this can save someone hours of debugging.

Top 3 video Explaining ajax - POST JSON fails with 415 Unsupported media type, Spring 3 mvc