package com.newfiber.api.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.newfiber.api.core.interceptor.JurInterceptor; import com.newfiber.api.core.interceptor.LoginInterceptor; import java.text.SimpleDateFormat; import java.util.ArrayList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import java.util.List; /** * MVC配置 * @author hy */ @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Autowired private LoginInterceptor loginInterceptor; @Autowired private JurInterceptor jurInterceptor; @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter; for (HttpMessageConverter<?> httpMessageConverter : converters) { if(httpMessageConverter instanceof MappingJackson2HttpMessageConverter){ jackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) httpMessageConverter; ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); jackson2HttpMessageConverter.setObjectMapper(objectMapper); converters.add(0,jackson2HttpMessageConverter);// 放到第一个 break; } } } @Override public void addInterceptors(InterceptorRegistry registry) { registry .addInterceptor(loginInterceptor) .addPathPatterns("/**") .excludePathPatterns("/logininfo/login","/logininfo/logout","/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**","/websocketpatrol/**"); registry .addInterceptor(jurInterceptor) .addPathPatterns("/**") .excludePathPatterns("/logininfo/login","/logininfo/logout","/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**","/websocketpatrol/**"); super.addInterceptors(registry); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); //设置日期格式 ObjectMapper objectMapper = new ObjectMapper(); SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(smt); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); converters.add(mappingJackson2HttpMessageConverter); super.configureMessageConverters(converters); } /** * 配置静态访问资源 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); super.addResourceHandlers(registry); } }