1.导入依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.51</version>
</dependency>
2.创建管理类WebConfig
@Configuration
public class WebConfig {
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		
		fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
		HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
		
		return new HttpMessageConverters(converter);
	}
}3.创建测试实体类
public class Test_Table {
    private Integer id;
    private String username;
    private Date birthday;
}getter 和 setter 此处省略。
4.创建Controller
@Controller
@RequestMapping("fastjson")
public class FastJsonController{
    @RequestMapping("/test")
    @ResponseBody
    public Test_Table test() {
        Test_Table test_table = new Test_Table();
        test_table.setId(1);
        test_table.setUsername("汤姆");
        test_table.setBirthday(new Date());
        return test_table;
    }
}结果如下:

乱码,在@RequestMapping添加
 @RequestMapping(value = "/test",produces="text/html;charset=UTF-8")
日期是毫秒数,我们在 User 类中使用 Fastjson 的注解,如下内容:
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date birthday;再次测试,结果如下:

当日期格式与我们修改的内容格式一致,说明 Fastjson 整合成功。
                
            
        
            
评论 (0)