如何通過注解方式給項目添加Swagger功能


   在Java后端,每次開發一個新的接口后需要自測,此時可以借助Swagger功能很好地完成自測,下面將通過注解的方式來添加Swagger。

  (1)代碼部分

 1 package com.bien.edge;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import springfox.documentation.builders.ApiInfoBuilder;
 8 import springfox.documentation.builders.ParameterBuilder;
 9 import springfox.documentation.builders.PathSelectors;
10 import springfox.documentation.builders.RequestHandlerSelectors;
11 import springfox.documentation.schema.ModelRef;
12 import springfox.documentation.service.ApiInfo;
13 import springfox.documentation.service.Parameter;
14 import springfox.documentation.spi.DocumentationType;
15 import springfox.documentation.spring.web.plugins.Docket;
16 import springfox.documentation.swagger2.annotations.EnableSwagger2;
17 
18 @Configuration
19 @EnableSwagger2
20 public class Swagger2 {
21 
22     @Bean
23     public Docket createRestApi() {
24         ParameterBuilder userid = new ParameterBuilder();
25         ParameterBuilder username = new ParameterBuilder();
26         ParameterBuilder lang = new ParameterBuilder();
27         List<Parameter> pars = new ArrayList<Parameter>();
28         
29         // 添加默認請求頭
30         userid.name("X-Person-No").description("人員號碼").modelRef(new ModelRef("string")).parameterType("header").required(false);
31         username.name("X-Person-Name").description("人員姓名").modelRef(new ModelRef("string")).parameterType("header").required(false);
32         lang.name("X-Lang-Id").description("語言標准編碼").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue("zh_CN");
33         
34         pars.add(userid.build());
35         pars.add(username.build());
36         pars.add(lang.build());
37         return new Docket(DocumentationType.SWAGGER_2)
38                 .apiInfo(apiInfo())
39                 .select()
40                 .apis(RequestHandlerSelectors.basePackage("com.bien.edge"))
41                 .paths(PathSelectors.any())
42                 .build().globalOperationParameters(pars);
43 
44     }
45 
46     private ApiInfo apiInfo() {
47         return new ApiInfoBuilder()
48                 .title("Bien94Edge項目API可視化")
49                 .description("Bien94Edge項目API可視化相關接口")
50                 .version("1.0")
51                 .build();
52     }
53 }

  (2)配置部分

  在application.yml文件中配置swagger的api訪問方式,如下圖所示:  

  

  其實,也可以不用配置這些屬性的,默認是api-docs,當前path值也可以設置為其它值,只要不與工程中其它路徑沖突即可,也就是說保持唯一性即可。

  默認時,鏈接類似於這樣的:http://localhost:1994/bien-edge/v2/api-docs

  若path設置為/swagger.json時,鏈接類似於這樣的: http://localhost:1994/bien-edge/swagger.json

  若path設置為/swaggerbien時,鏈接類似於這樣的: http://localhost:1994/bien-edge/swaggerbien

  (3)效果部分

  

  上圖是運行swagger后展示的一些相關說明信息,在上述代碼中apiInfo()函數進行設置即可。

  

  上圖所展示的是每個接口的默認請求頭信息,可以根據需要進行添加,並且還可以設置必須和非必須屬性。

  到此,項目中的Swagger功能添加完畢,是不是很簡單呀。^-^

   (4)其它

  當訪問swagger時,會有如下這些servlet path:

  • /
  • /swager-ui.html
  • /swagger-resources
  • /swagger-resources/configuration/ui
  • /swagger-resources/configuration
  • /swagger-resources/configuration/security
  • /error
  • /csrf
  • /webjars/springfox-swagger-ui/fonts/source-code-pro-v7-latin-600.woff2
  • /webjars/springfox-swagger-ui/fonts/titillium-web-v6-latin-regular.woff2
  • /webjars/springfox-swagger-ui/swagger-ui-bundle.js
  • /webjars/springfox-swagger-ui/springfox.js
  • /webjars/springfox-swagger-ui/swagger-ui.css
  • /webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js
  • /webjars/springfox-swagger-ui/springfox.css
  • /webjars/springfox-swagger-ui/favicon-32x32.png
  • /webjars/springfox-swagger-ui/favicon-16x16.png

  若是后端API添加了攔截器,那么訪問swagger的時候可能會訪問不了,此時就需要在攔截器中排除swagger的路徑就可以正常訪問了。只要排除這三個路徑就可以了:/swagger-ui.html、/swagger-resources/**和/webjars/springfox-swagger-ui/**

 

------20191231閃


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM