首页 > 其他分享 >SpringBoot集成Bootstrap-Swagger-ui,现在叫knife4j,功能更强大

SpringBoot集成Bootstrap-Swagger-ui,现在叫knife4j,功能更强大

时间:2022-10-21 16:13:33浏览次数:51  
标签:knife4j documentation SpringBoot Bootstrap springframework org import springfox 

离线文档格式很全面

 

 

集成方式

1、添加maven

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>

  

<!-- Swagger3依赖 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>3.0.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

 <!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.2</version>
        </dependency>

  

  



2、增加配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ktg"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                // 页面标题
                .title("MES系统API文档")
                // 项目描述
                .description("MES系统API文档")
                // 服务条款网址
                .termsOfServiceUrl("https://www.mes.com")
                // 作者信息
                .contact(new Contact("Admin","www.mes.com","[email protected]"))
                // 许可证
                .license("MIT")
                // 版本号
                .version("v1.0")
                .build();
    }
}

  

3、增加xx.doc.html映射

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class SwaggerBootstrapUiApplication  implements WebMvcConfigurer {

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

结束~~~

访问 http://localhost:8088/doc.html 

 

标签:knife4j,documentation,SpringBoot,Bootstrap,springframework,org,import,springfox,
From: https://www.cnblogs.com/xibb/p/16813793.html

相关文章