问题解决18:SpringBoot整合Swagger2的时候访问不到swagger-ul.html

版权声明:本文为博主原创文章,未经博主允许不得转载。如需要授权请联系[email protected] https://blog.csdn.net/leeue/article/details/89086851

环境使用的是SpringBoot2.0+Swagger2.8来整合的

因为项目需要配置一个解决跨域的问题,所以要定义一个CorsConfig来解决跨域问题
package com.travel.filter;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


/**
 * 跨域配置
 * @author liyue
 * @date 2019/4/8 13 16
 */
@Configuration
// @EnableWebMvc
public class CorsConfig extends WebMvcConfigurationSupport {


    @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/");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOrigins("*")
                // 是否允许证书 不再默认开启
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                // 跨域允许时间
                .maxAge(3600);
    }
}

问题解决方案如上 增加下面代码,映射到这个路径下 然后// @EnableWebMvc 注释掉,重启项目即可

  @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/");
    }

猜你喜欢

转载自blog.csdn.net/leeue/article/details/89086851
今日推荐