Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.
The Best Answer is You need to set the content-type in postman as JSON (application/json). Go to the body inside your POST request, there you will find the raw option. Right next to it, there will be a drop down, select JSON (application. json).
The jQuery ajax contenttype option is a built-in option that is passed to the ajax() function in the jQuery. The contenttype option is also called as MIME (multipurpose internet mail extension) type, it includes an HTTP header that specifies the information about what kind of data we are sending to the server.
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.
adding content type into the request as application/json
resolved the issue
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With