Spring MVC 415不支持的媒体类型

jac*_*ind 14 ajax jquery spring spring-mvc

我使用Spring 3.2并尝试使用ajax post请求提交json对象数组.如果这是相关的,我逃脱了所有特殊字符.

我的HTTP状态为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";
    }
Run Code Online (Sandbox Code Playgroud)

jquery是:

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");
                }
            });
        }
    });
Run Code Online (Sandbox Code Playgroud)

我发布的数组中的一个对象的示例是以下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"
}
Run Code Online (Sandbox Code Playgroud)

错误是:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种错误 - 有谁知道?

mut*_*thu 8

你可以试试HttpServletRequest.它没有任何问题

  @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";
    }
Run Code Online (Sandbox Code Playgroud)

  • `@RequestParam`可能也可以.并且在jQuery调用中,删除设置`contentType`:发送一个名为`profileCheckedValues`的数组(*发生*以保存JSON字符串,但可能是任何东西)不是`application/json`而只是默认的`应用程序/ x-WWW的形式urlencoded`. (2认同)

Swa*_*eel 6

1)添加以下依赖项

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson-version}</version> // 2.4.3
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson-version}</version> // 2.4.3
</dependency>
Run Code Online (Sandbox Code Playgroud)

2)如果在控制器方法中使用@RequestBody批注,请确保已在xml文件中添加以下内容

<mvc:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

这样可以解决415状态码的问题。