广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决@RequestBody接收json对象报错415的问题
  • 633
分享到

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

2024-04-02 19:04:59 633人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

@RequestBody接收JSON对象报错415 前端请求: $.ajax({ url: basePath() + "/index/login.do",

@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”,

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

--结束END--

本文标题: 解决@RequestBody接收json对象报错415的问题

本文链接: https://www.lsjlt.com/news/127773.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作