springboot使用注解配置filter以及对象转换

其实这部分配置就是从之前xml中提取出来,用注解的方式配置。

1、配置filter
方法一:
1)在application中添加注解@ServletComponentScan
2)写一个filter,用@WebFilter(urlPatterns = “/*”)注解标注

方法二:
1)写一个WebMvcConfig类继承WebMvcConfigurerAdapter
2)使用@EnableWebMvc,@Configuration注解
3)写一个filter的初始化方法,用@Bean注解
4)写一个FilterRegistrationBean的初始化方法,用@Bean注解,在初始化方法中配置之前的filter。
代码见后文。

2、配置拦截器
1)在之前的config类中,写一个拦截器初始化方法,用@Bean注解
2)重写addInterceptors方法, 在其中配置拦截路径以及不拦截 路径
代码见后文。

3、配置对象转换
见后文代码,配置了json格式转换为对象, 已经时间统一格式转换。

@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{

    @Bean
    public HandlerInterceptorAdapter handlerInterceptorAdapter(){
        return  new HandlerInterceptorAdapter();
    }

    @Bean
    public FilterRegistrationBean characterEncodingFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(characterEncodingFilter());
        //拦截规则
        registration.addUrlPatterns("/*");
        //过滤器名称
        registration.setName("CharacterEncodingFilter");
        //过滤器顺序
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public CharacterEncodingFilter characterEncodingFilter(){
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        List supportedMediaTypes = new ArrayList<>();
        //支持的content-type
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        converter.setSupportedMediaTypes(supportedMediaTypes);
        converter.setObjectMapper(objectMapper());
        return converter;
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));//设置时区
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        //把空的数组转为null
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,true);
//特殊的对象属性转化或处理,自定义
//objectMapper.setSerializerFactory(objectMapper.getSerializerFactory().withSerializerModifier());
        return objectMapper;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(handlerInterceptorAdapter()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Override
    public void configureMessageConverters(List> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
        super.configureMessageConverters(converters);
    }

}

也可以在xml文件中配置,在application中使用注解@ImportResource(“classpath:beanRefContext.xml”),配置引入的xml文件。
在此xml文件中配置filter,interceptor,对象转换

这里额外记录一下对象转换的xml配置


		
			
				
					
						text/html;charset=UTF-8
						application/json;charset=UTF-8
					
				
				
					
						
							
								
							
						
					
				
			
		
	

你可能感兴趣的:(spring,微服务)