package com.izouma.nineth.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.izouma.nineth.annotations.MobileRestController; import com.izouma.nineth.annotations.NoLoginRestController; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import java.util.List; @Configuration @EnableConfigurationProperties(GeneralProperties.class) public class WebMvcConfig implements WebMvcConfigurer { @Value("${storage.local_path}") private String localPath; @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.addPathPrefix("/app",c -> c.getPackage().isAnnotationPresent(MobileRestController.class)); configurer.addPathPrefix("/open",c -> c.getPackage().isAnnotationPresent(NoLoginRestController.class)); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); // registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/admin/**").addResourceLocations("classpath:/static/admin/"); registry.addResourceHandler("/files/**").addResourceLocations("file:" + localPath); registry.addResourceHandler("/MP_verify*").addResourceLocations("classpath:/"); } @Bean public Docket createApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() .title("接口文档") .version("1.0.0") .termsOfServiceUrl("#") .description("接口文档") .build()) .select() .apis(RequestHandlerSelectors.basePackage("com.izouma.nineth.web")) .paths(PathSelectors.any()) .build(); } @Override public void configureMessageConverters(List> converters) { converters.stream().filter(converter -> converter instanceof MappingJackson2HttpMessageConverter) .forEach(converter -> { ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); ((MappingJackson2HttpMessageConverter) converter).setObjectMapper(objectMapper); }); System.out.println(converters); } // @Bean // public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() { // MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); // //设置日期格式 // ObjectMapper objectMapper = new ObjectMapper(); // objectMapper.setDateFormat(CustomDateFormat.instance); // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); // //设置中文编码格式 // List list = new ArrayList<>(); // list.add(MediaType.APPLICATION_JSON_UTF8); // mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list); // return mappingJackson2HttpMessageConverter; // } // @Override // public void addFormatters(FormatterRegistry registry) { // DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); // registrar.setUseIsoFormat(true); // registrar.registerFormatters(registry); // } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedHeaders("*") .allowCredentials(true) .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH") .exposedHeaders("Content-Disposition"); } }