Spring MVC @RequestBody不能与jQuery Ajax一起使用? [英] Spring MVC @RequestBody not working with jquery ajax?

查看:79
本文介绍了Spring MVC @RequestBody不能与jQuery Ajax一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ajax请求

var dataModel = {name1:"value1", name2:"value2"};

$.ajax({
              url: "/testURL",
              type: "POST",
              async: false,
              contentType: "application/json",
              data: dataModel,
              success: function(response) {

              }

    })

这是我来自spring xml的相关代码段

Here is my relevant snippet from spring xml

<annotation-driven>
        <!-- Message converters are added to use custom object mapper in  MappingJackson2HttpMessageConverter.
            StringHttpMessageConverter is required to avoid MappingJackson2HttpMessageConverter from converting a string into json.
        -->
        <message-converters>
            <beans:bean
                class="org.springframework.http.converter.StringHttpMessageConverter">
            </beans:bean>
            <beans:bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <beans:property name="objectMapper" ref="jacksonObjectMapper"/>
            </beans:bean>
        </message-converters>
    </annotation-driven>

这是我的控制器映射

    @RequestMapping(value = "/testURL", method = { RequestMethod.POST })
    public String add(HttpServletRequest request, @RequestBody CustomObject customObject) throws Exception {}

但是我的请求甚至没有到达控制器.一旦删除@RequestBody CustomObject customObject,它就会起作用.但我想将json请求映射到 带有@RequestBody的CustomObject没有发生. 不确定我在这里想念什么吗?

But My request does not even reach to controller. As soon as I remove @RequestBody CustomObject customObject it works. But I want to map the json request to CustomObject with @RequestBody which is not happening . Not sure what i am missing here ?

实际上,当我检查request.getParameterMap()时,它显示为空,但是一旦我删除contentType: "application/json",我就会看到参数映射已填充,但仍然 然后出现以下错误

In fact when I inspect request.getParameterMap() it displays empty but as soon as I remove contentType: "application/json" I see parameter map gets populated but still then get below error

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

这是我的CustomObject定义

Here is my CustomObject definition

public class CustomObject implements Serializable {

private static final long serialVersionUID = 1L;
private String name1;
private String name2;

//getters and setters
}

已经通过 JQuery, Spring MVC @RequestBody和JSON-使其可以协同工作但没有帮助

推荐答案

实际上,当我检查request.getParameterMap()时,它显示为空,但 我删除contentType:"application/json"

In fact when I inspect request.getParameterMap() it displays empty but as soon as I remove contentType: "application/json"

是的.原因是使用contentType: "application/json" jQuery在内部将数据转换为字符串.因此没有请求参数.如果没有contentType: "application/json",则默认为contentType' is form data . So data sent is converted to request parameters based on delimiters& and =`

That is right. Reason is with contentType: "application/json" jquery internally convert the data into string. so there is no request parameter. Without contentType: "application/json" , default contentType' is form data . So data sent is converted to request parameters based on delimiters&and=`

也可以尝试data: JSON.stringify(dataModel),它应该可以工作

Also try data: JSON.stringify(dataModel), it should work

这篇关于Spring MVC @RequestBody不能与jQuery Ajax一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆