# 配置SwaggerConfig类,以及遇到的问题解决方式

配置SwaggerConfig类,以及遇到的问题解决方式

问题报错如下,访问页面404错误未找到路径

2023-04-01 11:13:03.849 WARN 6520 — [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping for GET /swagger-ui.html
LoginInterceptor
2023-04-01 11:13:04.049 WARN 6520 — [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping for GET /swagger-ui.html
LoginInterceptor

**配置SwaggerConfig类。**和出现的问题 No mapping for GET /swagger-ui.html

首先确定不是版本问题,尽量使用低等级的版本,高版本可能不兼容,使用依赖

<!--swagger-->
<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>

先再application.properties文件中添加配置

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

添加了配置代码依然出现 No mapping for GET /swagger-ui.html

则要手动配置 在SwaggerConfig中实现implements WebMvcConfigurer的相应方法。如下

package com.example.springbootdemotow.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author zwj
 * @date 2021/4/19 16:00
 * @description
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定构建api文档的详细信息的方法:apiInfo()
                .apiInfo(apiInfo())
                .select()
                // 指定要生成api接口的包路径,com包下的所有API都交给Swagger2管理
                .apis(RequestHandlerSelectors.basePackage("com"))
                //使用了 @ApiOperation 注解的方法生成api接口文档
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any()).build();
                //可以根据url路径设置哪些请求加入文档,忽略哪些请求
    }

    /**
     * 设置api文档的详细信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("更多请关注http://www.baidu.com")
                .termsOfServiceUrl("http://www.baidu.com")
                .contact("sunf")
                .version("1.0")
                .build();
    }
}

实现 WebMvcConfigurer,重写内置的方法: addResourceHandlers(ResourceHandlerRegistry registry)方法
其中addResourceHandler() 添加的是访问路径,addResourceLocations()添加的是映射后的真实路径,映射的真实路径末尾必须加 / ,

点击访问即可

[127.0.0.1:8080/swagger-ui.html](

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值