问题描述

2020-02-13 19:32:04.322  WARN 109508 --- [p-nio-80-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

自定义返回JSON:

前端代码

$.ajax({url:"khRyPfGsController.do?saveRows",type:"post",data:JSON.stringify(result),dataType:"json",success:function(data){tip(data.msg);if(data.success){reloadTable();}}
})

后端代码

@RequestMapping(params = "saveRows")
@ResponseBody
public AjaxJson saveRows(@RequestBody List<Map<String, Object>> list) {//省略
}

问题分析

如果Content-Type设置为“application/x-www-form-urlencoded;charset=UTF-8”

无论是POST请求还是GET请求都是可以通过@RequestParam成功获取参数

但是如果前端POST请求中的body是Json对象的话,会报错误

[Content type 'application/json' not supported]

反之:

[Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

请求中传JSON时设置的Content-Type 如果是application/json或者text/json时,JAVA中request.getParameter("")怎么也接收不到数据。这是因为,Tomcat的HttpServletRequest类的实现类为org.apache.catalina.connector.Request(实际上是org.apache.coyote.Request)。

当前端请求的Content-Type是Json时,可以用@RequestBody这个注解来解决。

@RequestParam 底层是通过request.getParameter方式获得参数的,换句话说,@RequestParam 和request.getParameter是同一回事。因为使用request.getParameter()方式获取参数,可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。所以,@RequestParam可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。

@RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST。

@RequestBody接受的是一个json对象的字符串,而不是Json对象,在请求时往往都是Json对象,用JSON.stringify(data)的方式就能将对象变成json字符串。

解决方案

JS代码加上contentType:"application/json"即可

$.ajax({url:"khRyPfGsController.do?saveRows",type:"post",data:JSON.stringify(result),contentType:"application/json",dataType:"json",success:function(data){tip(data.msg);if(data.success){reloadTable();}}
})

参考文章

使用SpringBoot做Javaweb时,数据交互遇到的问题

https://blog.csdn.net/qq_22067469/article/details/84989900

Spring Boot——[Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]解决方案相关推荐

  1. Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8

    强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 最近在用restTemplate进行一次http请求时发现了报错(Error whil ...

  2. spring boot配置文件【application.yml】常见问题之一,未添加空格

    正确属性与值: (port:空格8000) server:port: 8000 错误属性与值:(port:8000) server:port:8000 spring boot配置文件[applicat ...

  3. “Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“解决方法

    项目接口返回 code: 500 data: null message: "Content type 'application/x-www-form-urlencoded;charset=U ...

  4. jmeter报“msg“:“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“的解决方法

    jmeter报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supp ...

  5. Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported

    问题描述: 我写了一个接口,这个接口参数是这样的 @ResponseBody @RequestMapping(value = "importData", method = Requ ...

  6. Spring Boot中的application.properties配置简介(二)

    Spring Boot中的application.properties配置简介(二)   根据上文的介绍,我们已经对Spring Boot中的application.properties配置有了一定的 ...

  7. 请求状态为200,前端报系统出错,后端日志报“Content type ‘application/octet-stream‘not supported“错误

    请求后端出现"Content type 'application/octet-stream'not supported"错误 错误描述: Content type 'applica ...

  8. Springboot 报错Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported

    SpringBoot错误解决 1.错误展示 2021-03-23 12:20:02.805 ERROR org.springframework.web.HttpMediaTypeNotSupporte ...

  9. 苹果微信推送服务器错误,接入WxPusher微信推送服务出现错误:Content type application/x...

    背景 使用WxPusher微信推送服务 ,可以及时的将服务的一些运行异常信息,发送到自己的微信上,方便了解服务的运行状态(PS:这个服务是免费的). 你可以在这里看到WxPusher微信推送服务的接入 ...

最新文章

  1. 2021年黑龙江省智能车邀请赛
  2. python合并pdf 加书签_使用Python批量合并PDF文件(带书签功能)
  3. 机器学习——前馈神经网络
  4. mysql网页化_页面化操作数据库
  5. html设置表格和div的距离,CSS/HTML Div调整大小和表格定位
  6. 斗地主AI算法——第十六章の样例分析
  7. sails mysql_sails项目创建与常用基础操作总结
  8. SQLAlchemy schema.Column
  9. Java学习中的不解
  10. 【AD】Altium Designer 原理图的绘制
  11. 全志h6对比晶晨s905_一决雌雄!64位芯片晶晨S905对比瑞芯微RK3368谁胜?
  12. webpack 编译stylus_webpack4 引入stylus和css, npm run build后.styl文件没有提取出来
  13. 中英文标点符号切换的组合键_(完整版)切换中英文标点快捷键
  14. 谈一谈post和get的区别
  15. 状态模式、有限状态机 Unity版本实现
  16. 手机桌面上的计算机为什么不能删除,为什么我的电脑无法删除C盘上的文件当删除 – 手机爱问...
  17. 大学《管理学》教材笔记
  18. 浪潮飞龙系列国产服务器
  19. UE、UI、UCD、UED?你知道你是干啥的吗
  20. linux系统下qt中文输入法,Ubuntu系统下Qt使用中文输入法(示例代码)

热门文章

  1. 使用read_html爬取网页表哥,Python笔记:用read_html()爬取table形式表格的网络数据...
  2. python中xlwt的局限,Python xlwt 生成Excel和设置特定单元格不可编辑
  3. html图片渐隐渐显,js实现图片切换效果渐隐渐显
  4. android 字体像素转换工具类_Android中px与dip,sp与dip等的转换工具类
  5. api接口加密_谈谈API接口开发中的安全性如何解决
  6. java 彩铃开发,motorola铃声算法,是java的!_移动短信技术
  7. mysql server安装不成功,解决Mysql5.7.17在windows下安装启动时提示不成功问题
  8. java http请求
  9. IC设计Linux设置文件夹颜色,IC设计中常用的Linux命令
  10. php编译优化,浅析使用Turck-mmcache编译来加速、优化PHP代码