Json 解析器
在Spring boot 2 中 默认的Json 解析器,是jackson,所以如果想使用fastjson当作解析器,需调设置转换器。
添加依赖
由于fastjson偶尔会出现先bug,建议使用最新版本。
第一种方式 Bean
第二种方式(fastjson 文档推荐,但WebMvcConfigurerAdapter类已过期)
注意:SpringBoot 2.0.1版本中加载WebMvcConfigurer的顺序发生了变动,故需使用converters.add(0, converter);指定FastJsonHttpMessageConverter在converters内的顺序,否则在SpringBoot 2.0.1及之后的版本中将优先使用Jackson处理
实例测试
创建 实体
创建 RestController
@RestControllerpublic class TestController { @RequestMapping("/json") public User fastJson() { return new User("this is fastjson"); }}
添加convert 访问
http://localhost:8080/json { "user_name":"this is fastjson" }
可以看到 userName已经变为下划线格式,说明配置已经生效。