spring boot 整合swagger2(knife4j)

空扰寡人 提交于 2020-03-16 10:48:49
1、当然是要引入jar包了
 https://doc.xiaominfo.com/guide/settings.html     官网链接
<!-- swagger2接口文档配置 这里使用2.5.0版本,这个版本没问题比较稳定,但是knife4j官网引入的版本有报错-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
</dependency>

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>
2、Swagger 配置文件
package com.sun.woofileyong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;

/**
 * @author xing.Li
 * @create 2020/03/13 15:24
 */
@Configuration
@EnableSwagger2
@Profile({"dev"}) //@Profile({"dev","{pro}"}) 这样写就是两个环境都显示-----设置要显示的工作环境(控制那个环境显示接口文档)
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 设置swagger-ui.html页面上的一些元素信息。
                .apiInfo(metaData())
                .select()
                // 扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.sun.woofileyong.controller"))
                // 定义要生成文档的Apiurl路径规则
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                //接口标题
                .title("woofileyong RESTful APIs")
                //副标题
                .description("woofileyong")
                //接口路径
                .termsOfServiceUrl("http://192.168.100.140:10001/")
                //创建人
                .contact("xing.Li")
                //版本
                .version("1.0")
                .build();
    }
    //配置放行资源
    @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:8080/doc.html    大致就长这样

注:还有另一种在生产环境中不显示文档接口的方案

博主连接:https://www.bbsmax.com/A/D854nBxYzE/

spring boot 多数据源配置

https://my.oschina.net/u/3774949/blog/3193333

spring boot通过maven控制数据源切换

https://my.oschina.net/u/3774949/blog/3195782

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!