What did I do wrong? I try to use Spring mvc and JSON. When I try to debug my code I am looking that javascript works but doesn't works controller. In browser I get error 415 Unsupported Media Type.
Script:
$(document).ready(function() {
$('#newSmartphoneForm').submit(function(event) {
var producer = $('#producer').val();
var model = $('#model').val();
var price = $('#price').val();
var json = { "producer" : producer, "model" : model, "price": price};
$.ajax({
url: $("#newSmartphoneForm").attr( "action"),
data: JSON.stringify(json),
type: "POST",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(smartphone) {
var respContent = "";
respContent += "<span class='success'>Smartphone was created: [";
respContent += smartphone.producer + " : ";
respContent += smartphone.model + " : " ;
respContent += smartphone.price + "]</span>";
$("#sPhoneFromResponse").html(respContent);
}
});
event.preventDefault();
});
});
Controllers:
@RequestMapping(value="/create", method=RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Smartphone createSmartphone(@RequestBody Smartphone smartphone) {
return smartphoneService.create(smartphone);
}
What did I do wrong? I try to use Spring mvc and JSON. When I try to debug my code I am looking that javascript works but doesn't works controller. In browser I get error 415 Unsupported Media Type.
Script:
$(document).ready(function() {
$('#newSmartphoneForm').submit(function(event) {
var producer = $('#producer').val();
var model = $('#model').val();
var price = $('#price').val();
var json = { "producer" : producer, "model" : model, "price": price};
$.ajax({
url: $("#newSmartphoneForm").attr( "action"),
data: JSON.stringify(json),
type: "POST",
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success: function(smartphone) {
var respContent = "";
respContent += "<span class='success'>Smartphone was created: [";
respContent += smartphone.producer + " : ";
respContent += smartphone.model + " : " ;
respContent += smartphone.price + "]</span>";
$("#sPhoneFromResponse").html(respContent);
}
});
event.preventDefault();
});
});
Controllers:
@RequestMapping(value="/create", method=RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Smartphone createSmartphone(@RequestBody Smartphone smartphone) {
return smartphoneService.create(smartphone);
}
Share
Improve this question
asked Jan 24, 2014 at 23:27
user3233853user3233853
4512 gold badges5 silver badges9 bronze badges
1
-
What Spring MVC version are you using? Open up your Network console. Are you seeing the
Content-Type
header being sent? Show us yourSmartphone
class. – Sotirios Delimanolis Commented Jan 24, 2014 at 23:50
1 Answer
Reset to default 5It may be happening because you do not have Jackson on your classpath at runtime.
The error message says that the server cannot handle your JSON request for some reason. JSON is converted to a Java object with a thing that is called message converter. If you have <mvc:annotation-driven />
in your Spring XML config (or you have Java Config enabled), the JSON message converter is registered automatically. If you don't, you have to register it.