【JAVA swagger】解决No mapping for GET /swagger-ui.html报错

完整代码在最后

一、报错
1.网页报错404
在这里插入图片描述2.代码报错
No mapping for GET /swagger-ui.html在这里插入图片描述

二、解决办法
1.版本回退
之前用的是swagger3.0.0和springboot3.0.6,始终没找到合适的解决办法,故将版本回退至swagger2.9.2和springboot2.7.11在这里插入图片描述

2.Spring Boot 2.6.X后与Swagger有版本冲突问题,需要在application.properties文件中写入spring.mvc.pathmatch.matching-strategy=ant_path_matcher
在这里插入图片描述
3.重写父类方法
在SwaggerConfig配置类中继承WebMvcConfigurer,然后重写addResourceHandlers方法

public class SwaggerConfig implements WebMvcConfigurer {
//    重写父类方法
    @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/");
    }
}

3.以上配置完亲测可用
在这里插入图片描述
小插曲:之前继承的是WebMvcConfigurationSupport,导致浏览器无法显示中文,我也是新手入门没搞懂为什么,换继承WebMvcConfigurer后完美解决。
在这里插入图片描述

三、完整代码

package com.example.bsdemo.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;

//告诉Spring容器,这个类是一个配置类
@Configuration
//启用Swagger2功能
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
//    重写父类方法
    @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/");
    }

    //  配置Swagger2相关的bean
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com"))  //com包下所有API都交给Swagger2管理
                .paths(PathSelectors.any()).build();
    }

    //  此处主要是API文档页面显示信息
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("演示项目API") //标题
                .description("学习Swagger2的演示项目") //描述
                .version("1.0") //版本
                .build();
    }
}

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值