Springboot要集成fastjson配合注解@ResponseBody实现返回数据格式json解析
首先引入pom依赖
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
然后加入消息转换器,实现json数据解析
@Bean
public HttpMessageConverters fastJsonConfigure(){
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 中文乱码解决方案
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
converter.setSupportedMediaTypes(mediaTypes);
//日期格式化
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
converter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(converter);
}
控制层代码(这里是封装了通用result类)
@GetMapping("/getProvince")
public Result getProvince()
{
Provinces provinces = iUserService.getProvince();
logger.info("省市--->"+provinces.getProvince()+provinces.getCity());
return ResultUtil.success(provinces);
}
返回结果