Java 拦截器配置swagger,解决No mapping for GET /web/swagger-ui.html问题

原因是在项目里自定义了拦截器,继承了WebMvcConfigurationSupport这个类,在配置文件.properties里配置了server.servlet.context-path=/web,正常在pom文件里引用了swagger依赖,

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

重启项目,浏览器 http://localhost:端口号/web/swagger-ui.html 这样是不能访问的,一方面是被拦截器拦截啦,另一个是没有配置映射关系,是找不到swagger-ui.html这个页面的。经过查找资料,正确写法如下,红色是swagger配置重点,写的不好,大家多指教

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private BossAuthIntercepter bossAuthIntercepter;

    // 配置映射关系
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
     
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");              registry.addResourceHandler("/swaggerui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    // 配置拦截规则
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(bossAuthIntercepter)
                .addPathPatterns("/**")   //拦截所有请求
                .excludePathPatterns("/error")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**",                                         "/swagger-ui.html/**");
    }

}

BossAuthIntercepter类是自己定义的拦截规则,在这里用不上,可以去掉。

ResourceHandlerRegistry类用于保存服务静态资源图片,css文件或者其他文件的资源处理器                                                   (resource handler)的注册信息

InterceptorRegistry类是拦截器,用于拦截访问的地址。

配置后,重启项目,浏览器登入http://localhost:端口号/web/swagger-ui.html,就可以访问啦

  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您在访问`/swagger-ui.html`时出现了`No mapping for GET`错误。这个错误通常是由于您的应用程序缺少相应的请求映射所引起的。 要解决这个问题,您可以尝试以下几个步骤: 1. 确保您的应用程序中已经正确配置Swagger相关的依赖项。您可以在`pom.xml`文件中添加以下依赖项: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 或者,如果您使用Gradle构建工具,则可以在`build.gradle`文件中添加以下依赖项: ```groovy implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' ``` 2. 确保您的应用程序中已经正确配置Swagger的相关配置类。您可以创建一个继承自`WebMvcConfigurationSupport`的配置类,并重写`addResourceHandlers`方法,将Swagger的资源路径添加到资源处理程序中。例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } } ``` 3. 确保您的应用程序中已经正确配置Swagger的API文档。您可以创建一个Swagger配置类,并使用`@Api`和`@ApiOperation`等注解来定义API接口和操作。例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 请根据您的具体情况检查以上步骤,并确保正确配置Swagger相关的依赖项、配置类和API文档。如果问题仍然存在,请提供更多详细信息以便我能够更好地帮助您解决问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值