SpringMVC过程中@RequestBody接收Json的问题 总是报415

简介: 在SpringMVC中用@RequestBody接收Json的问题,总是报415,经过一翻查找 前台js的post: var postdata = '{"title":"这是一个标题","describe":"这是一个描述"}'; $.

在SpringMVC中用@RequestBody接收Json的问题,总是报415,经过一翻查找

前台js的post:

var postdata = '{"title":"这是一个标题","describe":"这是一个描述"}';
            $.ajax({
                type : 'POST',
                contentType : 'application/json',
                url : '/home/requestbodybind',
                processData : false,
                dataType : 'json',
                data : postdata,
                success : function(data) {
                    alert('title : '+data.title+'\ndescribe : '+data.describe);
                },
                error : function() {
                    alert('error...');
                }
            });

该有的都有

后台:

 @RequestMapping(value = "modelautobind", method = RequestMethod.POST)
    public String modelAutoBind(HttpServletRequest request, @ModelAttribute("accountmodel") NewsModel newsModel, @ModelAttribute("sessionaccountmodel") NewsModel newsModel1) {
        newsModel.setTitle("视图");
        newsModel1.setTitle("session视图");
        //model.addAttribute("accountmodel", newsModel);
        return "modelautobind";//视图名字
    }

网上说需要加入 consumes = "application/json",加了 还是一样报错,后面去掉后也没影响

springmvc的配置文件中有:<mvc:annotation-driven />  

后来有查到对json的解析需要引入

     <dependency>  
            <groupId>com.fasterxml.jackson.core</groupId>  
            <artifactId>jackson-databind</artifactId>  
            <version>2.5.3</version>  
        </dependency>  

在springmvc配置中用得到

            <bean 
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
                <property name="objectMapper"> 
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper"> 
                        <property name="dateFormat"> 
                            <bean class="java.text.SimpleDateFormat"> 
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> 
                            </bean> 
                        </property> 
                    </bean> 
                </property> 
            </bean> 

加上重新跑,这时通过

以上就是整个解决过程。

 

目录
相关文章
|
5月前
|
JSON 前端开发 Java
SpringMVC之JSON返回及异常处理
SpringMVC之JSON返回及异常处理
63 0
|
9月前
|
JSON 数据格式
HttpClient封装类并接收POST请求传json参数
今天总结一下对于HttpClient的封装以及使用,便于以后的使用,这里我只封装了接收参数为Json形式的POST与GET两种方法,对于其他的方法大家可以随时留言,一起讨论。
218 0
|
9月前
|
JSON 应用服务中间件 数据格式
SpringMVC返回JSON格式数据
SpringMVC返回JSON格式数据
64 0
|
11月前
|
JSON 前端开发 JavaScript
springmvc中使用ResponseBody 响应 json 数据
springmvc中使用ResponseBody 响应 json 数据
|
JSON 数据格式 Python
如何使用Request方法发送Post请求、JSON文件介绍、Python中的JSON数据以及获取到的JSON数据的解析。
如何使用Request方法发送Post请求、JSON文件介绍、Python中的JSON数据以及获取到的JSON数据的解析。
|
JSON 前端开发 Java
SpringMVC 使用@ResponseBody返回json 中文乱码
SpringMVC 使用@ResponseBody返回json 中文乱码
211 0
SpringMVC 使用@ResponseBody返回json 中文乱码
|
JSON 数据格式
springMvc48-返回json数据
springMvc48-返回json数据
87 0
|
JSON Java fastjson
controller发送请求接收返回json
controller发送请求接收返回json
198 0
controller发送请求接收返回json
|
JSON 数据格式
SpringMVC将对象转化为JSON时406
SpringMVC将对象转化为JSON时406
137 0
SpringMVC将对象转化为JSON时406