解决@RequestBody接收json对象报错415的问题

@RequestBody接收json对象报错415

前端请求:

$.ajax({            url: basePath() + "/index/login.do",            type : "post",            data: JSON.stringify(form),            dataType : "json",            contentType : "application/json;charset=utf8",            success: function (data) {                console.log(data);            },            error: function () {             }        });

后端接收:

@ResponseBody @RequestMapping(value = "/login",method = RequestMethod.POST,produces = "application/json;charset=utf8") public JSONObject login(@RequestBody LoginVo loginVo){   JSONObject result = new JSONObject();  UsernamePasswordToken token = new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());  System.out.println(loginVo.isRememberMe());  Subject subject = SecurityUtils.getSubject();  subject.login(token);  if (subject.isAuthenticated()){   result.put("result",true);  }else{   result.put("result",false);  }  return result; }

前端ajax请求,后端使用@RequestBody接收,报出415请求数据格式错误

错误原因:

springMVC无法读取ajax设置好的dataType并以对应的方式处理请求头,进而无法处理json数据

解决办法:

在maven中引入Jackson相关jar包,并在springMVC的xml中引入相关配置,maven和springMVC的相关代码如下:

maven:

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

springMVC:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">        <property name="messageConverters">            <list>                <!-- 设置返回字符串编码 -->                <bean class="org.springframework.http.converter.StringHttpMessageConverter">                    <property name = "supportedMediaTypes">                        <list>                            <value>text/html;charset=UTF-8</value>                            <value>application/json;charset=UTF-8</value>                        </list>                    </property>                </bean>                <!-- json转换器 -->                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                    <property name="supportedMediaTypes">                        <list>                            <value>text/html;charset=UTF-8</value>                            <value>application/json;charset=UTF-8</value>                        </list>                    </property>                </bean>            </list>        </property>    </bean>

后端使用@RequestBody接收前端传来的数据

踩坑①

@RequestBody接收json字符串,只能使用post的提交方式

前端直接复制了相似功能页面的js,该页面是使用的get的提交方式

但前端报错500,后端报错提示

2019-09-12 09:17:43.088 ERROR GlobalExceptionHandler : An exception occurs within the system : Required String parameter ‘xxx’ is not present

踩坑②

后将.get(URL,data,callback)修改为.post(URL,data,callback);

$.post(URL,data,callback);

必需的 URL 参数规定您希望请求的 URL。

可选的 data 参数规定连同请求发送的数据。

可选的 callback 参数是请求成功后所执行的函数名

但前端继续报错500,后端报错提示

2019-09-12 09:23:15.409 ERROR GlobalExceptionHandler : An exception occurs within the system : Content type ‘application/x-www-form-urlencoded;charset=UTF-8′ not supported

踩坑③

后端提示不支持Content type 为’application/x-www-form-urlencoded;charset=UTF-8’的格式,百度查了一下.post(URL,data,callback)只是预配置.ajax调用的快捷方式,并不能修改contentType的类型

所以将$.post方法修改为了&.ajax方法

设置

type: “post”,url: ctx + url,data: JSON.stringify(allData),dataType: “json”,contentType:“application/json;charset=utf-8”,

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

问候不一定要慎重其事,但一定要真诚感人

解决@RequestBody接收json对象报错415的问题

相关文章:

你感兴趣的文章:

标签云: