admin管理员组文章数量:1325326
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.
本文标签: javaJSON plus spring mvc 32 error 415 (Unsupported Media Type)Stack Overflow
版权声明:本文标题:java - JSON plus spring mvc 3.2 error 415 (Unsupported Media Type) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742197496a2431346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论