SpringBoot 使用fastjson

spring boot默认使用的json解析框架是jackson,替换为fastjson有两种方式

1.继承WebMvcConfigurerAdapter

@SpringBootApplication
public class App extends WebMvcConfigurerAdapter{
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
    
        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();    
        FastJsonConfig fastJsonConfig=new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastConverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

2.使用@Bean注入方式

@Configuration
public class WebMvcConfig{

    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(){
        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();        
     FastJsonConfig fastJsonConfig=new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastConverter.setFastJsonConfig(fastJsonConfig); HttpMessageConverter<?> converter=fastConverter; return new HttpMessageConverters(converter); } }

 

posted @ 2018-05-09 09:34  April.Chen  阅读(364)  评论(0编辑  收藏  举报