1继承WebMvcConfigurerAdapter类,并且重写里面的configureMessageConverters方法
public class App extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//定义一个convert转化消息对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter=new FastJsonHttpMessageConverter();
//定义一个config对象,设置配置信息
FastJsonConfig config=new FastJsonConfig();
config.setSerializerFeatures(
SerializerFeature.PrettyFormat);
//将配置信息添加到converter中
fastJsonHttpMessageConverter.setFastJsonConfig(config);
//将converter添加到converters中
converters.add(fastJsonHttpMessageConverter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2使用@Bean注解
@SpringBootApplication
public class App {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
//定义一个converter 转化消息对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息,例如格式
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//将配置信息添加到converter中
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}