Spring MVC的415不支持的媒体类型 [英] Spring MVC 415 Unsupported Media Type
本文介绍了Spring MVC的415不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Spring 3.2,并尝试使用一个ajax post请求提交JSON对象的数组。如果是相关的,我逃所有特殊字符。
I am using Spring 3.2 and try to use an ajax post request to submit an array of json objects. If this is relevant, I escaped all special characters.
我收到了415的HTTP状态
I am getting an HTTP Status of 415.
我的控制器是:
@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json")
public @ResponseBody String saveProfileJson(@RequestBody String[] profileCheckedValues){
System.out.println(profileCheckedValues.length);
return "success";
}
jQuery是:
jquery is:
jQuery("#save").click(function () {
var profileCheckedValues = [];
jQuery.each(jQuery(".jsonCheck:checked"), function () {
profileCheckedValues.push($(this).val());
});
if (profileCheckedValues.length != 0) {
jQuery("body").addClass("loading");
jQuery.ajax({
type: "POST",
contentType: "application/json",
url: contextPath + "/sample/save-profile",
data: "profileCheckedValues="+escape(profileCheckedValues),
dataType: 'json',
timeout: 600000,
success: function (data) {
jQuery('body').removeClass("loading");
},
error: function (e) {
console.log("ERROR: ", e);
jQuery('body').removeClass("loading");
}
});
}
});
和从阵列我张贴是以下JSON的一个对象的示例:
and an example of one of the objects from the array I am posting is the following json:
{
"id": "534213341",
"name": "Jack Lindamood",
"first_name": "Jack",
"last_name": "Lindamood",
"link": "https://www.facebook.com/jack",
"username": "jack",
"gender": "male",
"locale": "en_US",
"updated_time": "2013-07-23T21:13:23+0000"
}
该错误是:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
为什么这个错误发生? - 没有人知道
Why is this error happening - does anyone know?
推荐答案
您可以使用的HttpServletRequest
尝试。它没有任何问题
You may try with HttpServletRequest
. it does not have any problem
@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json",headers = "content-type=application/x-www-form-urlencoded")
public @ResponseBody String saveProfileJson(HttpServletRequest request){
System.out.println(request.getParameter("profileCheckedValues"));
return "success";
}
这篇关于Spring MVC的415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文