springboot3集成knife4j出现http://localhost:8082/doc.html 404 (Not Found)404 (Not Found)

问题:如题


原因:如果你的config也继承了WebMvcConfigurationSupport,像我的一样

@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags()!=null){
                openApi.getTags().forEach(tag -> {
                    Map<String,Object> map=new HashMap<>();
                    map.put("x-order", 1);
                    tag.setExtensions(map);
                });
            }
            if(openApi.getPaths()!=null){
                openApi.addExtension("x-test123","333");
                openApi.getPaths().addExtension("x-abb",1);
            }
        };
    }


    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("愈站API接口文档")
                        // 接口文档简介
                        .description("这是基于Knife4j OpenApi3的接口文档")
                        // 接口文档版本
                        .version("v1.0")
                        // 开发者联系方式
                        .contact(new Contact().name("JasonSteve").email("19938943480@163.com")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringBoot基础框架")
                        .url("http://127.0.0.1:8088"));
    }

    /**
     * 设置静态资源映射
     * @param registry
     */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


//    //以下分组可省略
//    @Bean
//    public GroupedOpenApi systemApi() {
//        return GroupedOpenApi.builder().group("hello系统模块")
//                .pathsToMatch("/hello/**")
//                .build();
//    }
//
//    @Bean
//    public GroupedOpenApi authApi() {
//        return GroupedOpenApi.builder().group("goodbye模块")
//                .pathsToMatch("/goodbye", "/login")
//                .build();
//    }

}

同时控制台也会报出如下WARN:

2024-01-09T15:42:20.677+08:00  WARN 6924 --- [quick_start] [nio-8082-exec-3] o.s.web.servlet.PageNotFound             : No mapping for GET /doc.html
2024-01-09T15:42:20.678+08:00  WARN 6924 --- [quick_start] [nio-8082-exec-3] o.s.web.servlet.PageNotFound             : No endpoint GET /doc.html.

​​​​​​​有的博主提到原因是因为继承了WebMvcConfigurationSupport后yml配置文件就会失效(但我发现更改yml的配置后,doc.html显示的内容也会变)

这时需要配置资源映射器在SwaggerConfig中加入如下代码

    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

结果恢复正常

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值