在Spring boot 中使用 @RequestBody 会报错,前端访问提示错误 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
,代码如下:
@RequestMapping(value = "/getReadNum", method= RequestMethod.POST)
public CZResult getReadNum(@RequestBody TbBlog blog){
return readNService.getReadNum(blog);
}
我在网上找了好多文章,出现这个错误,需要引入SpringMVC 默认的解析工具Jackson。我引入一下的包,如
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
但是还是没有解决问题,后来我才发现,在JDK8项目中使用JDK8新特性的话,jackson需要新增依赖包。如下
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.6</version>
</dependency>
然后就完美解决这个问题了。
参考文章:
SpringBoot @RequestBody 报错 ('application/x-www-form-urlencoded;charset=UTF-8' not supported)
https://blog.csdn.net/chenfei2341/article/details/83652586
spring boot 最佳实践(一)--使用jackson
https://blog.csdn.net/swordcenter/article/details/72368905
参考以上的文章,经过自己测试,终于找到了解决方案,特意在留下足迹。
spring boot的controller层入参添加@RequestBody报('application/x-www-form-urlencoded;charset=UTF-8' not supported)blog.xungong68.com