Spring Boot 配置FastJson 框架
1. pom.xml 引入fastjson依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2. 添加配置类
@Configuration
public class InitialConfig {
/**
* 使用阿里 FastJson 作为JSON MessageConverter <br />
*
* <a href="http://www.cnblogs.com/zf29506564/p/6669870.html">fastjson SerializerFeature详解</a>
*
* @return
*/
@Bean
public HttpMessageConverters httpMessageConverters() {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.DisableCircularReferenceDetect
);
converter.setFastJsonConfig(config);
return new HttpMessageConverters(converter);
}
}
3. Controller,返回javaBean
@RestController
@RequestMapping("/sysuser")
public class SysuserController extends BaseController {
/**
* 根据id获取用户信息
*
* @param id
* @return
*/
@GetMapping("/{id}/detail")
public ResultBean get(@PathVariable(required = false) String id) {
Sysuser sysuser = sysuserService.findById(id);
return new ResultBean(sysuser);
}
}
4. 展示