学习知识不能只学当前的,学过要学会总结,本人通过学习spring boot 的第三方json解析框架,触类旁通,总结自定义配置spring boot 其他组件的使用,总结于此方便自己以后复习使用。如有大牛路过还请多多批评指正。
总结:
1:在pom.xml中引入相关的jar包信息,比如这里的FastJson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
2:让配置类继承WebMvcConfigurerAdapter(即让@SpringBootApplication注解标记的类实现WebMvcConfigurerAdapter)
3:配置组件的两种方法(一种是重写相对应的方法,另一种是注入组件Bean,如,FastJson的两种配置信息方法为)
@SpringBootApplication
@ComponentScan("com.cullinans.project")
public class ControllerApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args){
SpringApplication.run(ControllerApplication.class,args);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/");
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
*
1:定义一个Convert转换消息对象
2:添加fastjson配置信息
3:在convert中添加配置信息
4:将convert添加到converts中
* *
FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig= new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
converter.setFastJsonConfig(fastJsonConfig);
converters.add(converter);
}
@Bean
public HttpMessageConverters fastJsonHttpMessageConverts(){
FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();
FastJsonConfig config=new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
converter.setFastJsonConfig(config);
HttpMessageConverter messageConverter=converter;
return new HttpMessageConverters(messageConverter);
}
}