Bootstrap

整合Swagger的ui界面(knife4j)

官方文档:https://doc.xiaominfo.com/knife4j
版本兼容说明:https://doc.xiaominfo.com/docs/quick-start/start-knife4j-version
升级说明:https://doc.xiaominfo.com/docs/upgrading/upgrading-to-v4

版本兼容惯关系:
springboot 1.5.x~2.0.0 对应 <Knife4j 2.0.0
springboot 2.0 ~ 2.2 对应 Knife4j 2.0.0 ~ 2.0.6
springboot 2.2.x~2.4.0 对应 Knife4j 2.0.6 ~ 2.0.9
springboot 2.4.0~2.7.x 对应 >=Knife4j 4.0.0

引入依赖

knife4j依赖一定要和springboot对应版本对应, 不然报错

     <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.9</version>
        </dependency>

swagger相关文档可以看我的另一篇文章

Swagger配置详解

WebMVCConfig中增加knife4j显示文档

报错信息, 添加WebMVCConfig并配置knife4j 显示文档
image.png

@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        /** 配置knife4j 显示文档 */
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        /**
         * 配置swagger-ui显示文档
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        /** 公共部分内容 */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

访问与测试

地址后加/doc.html#/home
http://localhost:8082/doc.html#/home
image.png

;