集成Swagger2,接口文档不显示

时间:2025-03-16 08:17:18
package com.chengshan.camera.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; /** * MvcConfig * * @author fb * @Description 解决跨域、swagger拦截等配置 * @date 2020-01-10 */ @Configuration @EnableWebMvc public class MvcConfig implements WebMvcConfigurer { /** * Configure how long in seconds the response from a pre-flight request can be cached by clients. */ @Value("${:3600}") private Integer maxAge; /** * Add handlers to serve static resources such as images, js, and, css * files from specific locations under web application root, the classpath, * and others. * * @param registry Stores registrations of resource handlers for serving static resources such as images, css files and others */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/*", "*/*", "/webjars/**", "/static/*", "templates/*") .addResourceLocations("classpath:/META-INF/resources/", "classpath:/META-INF/resources/webjars/", "classpath:/static/", "classpath:/templates/"); } /** * Configure cross origin requests processing. * 开启跨域 * * @param registry Assists with the registration of global, URL pattern based * @since 4.2 * <p> * SpringBoot升级后,跨域配置报错,将.allowedOrigins替换成.allowedOriginPatterns即可。 * When allowCredentials is true, allowedOrigins cannot contain thespecial value "*"since that cannot be set on the “Access-Control-Allow-Origin” response header. To allow credentials to a set of origins, list them explicitly or consider using “allowedOriginPatterns” instead. * 大概意思是:allowCredentials为true时,allowedOrigins不能包含特殊值“*”,因为不能在“Access Control Allow Origin”响应头上设置该值。要允许凭据指向一组源,请显式列出它们,或者考虑改用“allowedOriginPatterns”。 * 所以解决办法:将.allowedOrigins替换成.allowedOriginPatterns即可。 */ @Override public void addCorsMappings(CorsRegistry registry) { // 设置允许跨域的路由 registry.addMapping("/**") // 所有的当前站点的请求地址,都支持跨域访问。 // 设置允许跨域请求的域名 .allowedOriginPatterns("*") // 所有的外部域都可跨域访问。 如果是localhost则很难配置,因为在跨域请求的时候,外部域的解析可能是localhost、127.0.0.1、主机名 // 是否允许证书(cookies) .allowCredentials(true) // 是否支持跨域用户凭证 // 设置允许的方法,当前站点支持的跨域请求类型是什么 .allowedMethods(HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.DELETE.name(), HttpMethod.PUT.name(), HttpMethod.PATCH.name()) // 跨域允许时间 .maxAge(maxAge); } /** * Extend or modify the list of converters after it has been, either * {@link #configureMessageConverters(List) configured} or initialized with * a default list. * <p>Note that the order of converter registration is important. Especially * in cases where clients accept {@link MediaType#ALL} * the converters configured earlier will be preferred. * * @param converters the list of configured converters to be extended * @since 4.1.3 */ @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { WebMvcConfigurer.super.extendMessageConverters(converters); converters.forEach(httpMessageConverter -> { if (httpMessageConverter instanceof AbstractJackson2HttpMessageConverter) { ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter) httpMessageConverter).getObjectMapper(); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); } }); } }

相关文章