天天看點

springboot 內建 swagger-bootstrap-ui 生成背景接口文檔

前面學習了兩種 springboot 裡面使用 swagger 的方法,一種原生的,一種大佬封裝好的帶 vue樣式的,最近又學習到一種帶 bootstrap 樣式的,覺得還不錯,這裡分享出來
基于Swagger開發的Swagger-Bootstrap-UI

目錄結構

springboot 內建 swagger-bootstrap-ui 生成背景接口文檔

上代碼

一、引入 swagger-bootstrap-ui 的依賴

<!-- springboot  web 相關依賴-->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-web</artifactId>
			</dependency>

		<!--swagger-api 依賴開始
		由于是springfox-swagger的增強UI包,是以基礎功能依然依賴Swagger,springfox-swagger的jar包必須引入
-->
		<dependency>
			    <groupId>io.springfox</groupId>
			    <artifactId>springfox-swagger2</artifactId>
			    <version>2.9.2</version>
		</dependency>
		<dependency>
			    <groupId>com.github.xiaoymin</groupId>
			    <artifactId>swagger-bootstrap-ui</artifactId>
			    <version>1.9.2</version>
		</dependency>
           

二、編寫 swagger 的配置檔案

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI 可以使用EnableSwaggerBootstrapUI 的一些增強功能
public class SwaggerConfig {

    //分子產品建立不同分組文檔
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("基礎子產品")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wxw.springboot_swagger_bootstrapui.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    @Bean
    public Docket createMonitorRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("登入子產品")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wxw.springboot_swagger_bootstrapui.loginController"))
                .paths(PathSelectors.any())
                .build();
    }
    @Bean
    public Docket createActivitiRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("測試子產品")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wxw.springboot_swagger_bootstrapui.testController"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("背景接口文檔")
                .description("背景接口文檔描述")
                .termsOfServiceUrl("http://www.wxw.com/")
                .contact("wxw")
                .version("1.0")
                .build();
    }

           

三、controller 中加入相關使用注解

@RestController
@RequestMapping("/test")
@Api(tags = "測試", description = "測試描述", value = "測試")
public class TestController {

    @GetMapping
    @ApiOperation(value = "測試", notes = "測試")
    public String test() {
        return "hello,swagger";
    }
}
           

四、通路文檔頁面

swagger-bootstrap-ui預設通路位址是:http:// h o s t : {host}: host:{port}/doc.html

springboot 內建 swagger-bootstrap-ui 生成背景接口文檔

這樣文檔就出來了,文檔中的功能就可以自己慢慢研究了

五、配置安全相關功能

1、比如說生産環境我們不需要開啟文檔

yml 檔案中加入下面内容

# 如果需要屏蔽 swagger 資源
swagger:
  production: true
           

再次通路浏覽器文檔位址,會輸出這樣的内容

springboot 內建 swagger-bootstrap-ui 生成背景接口文檔

2、配置使用者名密碼才能通路文檔

swagger:
  basic:
#  開啟 認證功能
   enable: true
#   如果未配置使用者名密碼預設:admin/123321
   ## Basic認證使用者名
   username: wxw
   ## Basic認證密碼
   password: 123

           

關注公衆号檢視更多資源

springboot 內建 swagger-bootstrap-ui 生成背景接口文檔
springboot 內建 swagger-bootstrap-ui 生成背景接口文檔

繼續閱讀