使用SpringBoot项目继承Knif4j访问doc.html页面,出现No mapping for GET /doc.html

项目原因:

最近搭建一个新的SpringBoot项目,需要配置knif4j,从其他项目里拷过来knife4j版的Swagger配置文件,结果打开doc.html显示404,如下图:

在百度一顿搜,始终没解决。官网推荐的方法是:https://doc.xiaominfo.com/guide/springboot-404.html

出错原因:

因为doc.html是在jar包里的,需要使用资源处理器注册静态资源。

复制代码:

@SpringBootApplication
@EnableWebMvc
public class TestApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

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

}

问题解决

 

Sure! To integrate Spring Boot with Knife4j, you can follow these steps: 1. Add the required dependencies to your project's `pom.xml` file: ```xml <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> ``` 2. Create a new configuration class, typically named `SwaggerConfiguration`, and annotate it with `@Configuration`: ```java @Configuration public class SwaggerConfiguration { } ``` 3. Inside the `SwaggerConfiguration` class, add the necessary bean definitions for `Docket` and `ApiInfo`: ```java @Configuration public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) // Set your controller package .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Your API Documentation") .description("Description of your API") .version("1.0.0") .build(); } } ``` 4. Make sure to update the `basePackage` value in the `RequestHandlerSelectors` to match the package where your Spring MVC controllers are located. 5. Run your Spring Boot application, and you should be able to access the Knife4j swagger UI at `http://localhost:8080/doc.html`, where `8080` is your application's port. That's it! You have now integrated Knife4j with your Spring Boot application. You can customize the Swagger configuration further according to your requirements.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值