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<MediaType> supportedMediaTypes = new ArrayList<>();
//⽀持的content-type
supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
supportedMediaTypes.add(MediaType.TEXT_HTML);
converter.setSupportedMediaTypes(supportedMediaTypes);
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.TimeZone("GMT+8"));//设置时区
//把空的数组转为null
//特殊的对象属性转化或处理,⾃定义
/
/objectMapper.SerializerFactory().withSerializerModifier());
return objectMapper;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(handlerInterceptorAdapter()).addPathPatterns("/**");
super.addInterceptors(registry);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
}
}
也可以在xml⽂件中配置,在application中使⽤注解@ImportResource(“l”),配置引⼊的xml⽂件。在此xml⽂件中配置filter,interceptor,对象转换
这⾥额外记录⼀下对象转换的xml配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="mappingJacksonHttpMessageConverter" class="org.verter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper">
<bean class="dehaus.jackson.map.ObjectMapper">
<property name="dateFormat">
<bean class="SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
filter过滤对象数组</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论