程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring MVC 415不支持的媒体类型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring MVC 415不支持的媒体类型?

开发过程中遇到Spring MVC 415不支持的媒体类型的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring MVC 415不支持的媒体类型的解决方法建议,希望对你解决Spring MVC 415不支持的媒体类型有所启发或帮助;

你可以尝试使用httpServletrequest@H_675_4@。它没有任何问题

  @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";
    }
@H_675_4@

解决方法

我正在使用Spring 3.2,并尝试使用ajax发布请求来提交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";
    }
@H_675_4@

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");
                }
            });
        }
    });
@H_675_4@

我发布的数组中的对象之一的示例是以下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"
}
@H_675_4@

错误是:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
@H_675_4@

为什么会发生此错误-有人知道吗?

大佬总结

以上是大佬教程为你收集整理的Spring MVC 415不支持的媒体类型全部内容,希望文章能够帮你解决Spring MVC 415不支持的媒体类型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。