本文介绍了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("#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 不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 18:58