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


SpringBoot 项目出错: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

思路一:

如果Content-Type设置为“application/x-www-form-urlencoded;charset=UTF-8”无论是POST请求还是GET请求都是可以通过这种方式成功获取参数,但是如果前端POST请求中的body是Json对象的话,会报上述错误。

请求中传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字符串。

前端请求传Json象则后端使用@RequestParam

前端请求传Json对象的字符串则后端使用@RequestBody

相关阅读 >>

spring、springmvc、springboot和springcloud的区别

content type 'application/x-www-form-urlencoded;charset=utf-8' not supported

更多相关阅读请进入《Content-Type》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...