SpringBoot整合Swagger2及使用

簡介

swagger是一個流行的API開發框架,這個框架以「開放API聲明」(OpenAPI Specification,OAS)為基礎, 對整個API的開發周期都提供了相應的解決方案,是一個非常龐大的項目(包括設計、編碼和測試,幾乎支持所有語言)。

springfox大致原理:

springfox的大致原理就是,在項目啟動的過種中,spring上下文在初始化的過程, 框架自動跟據配置加載一些swagger相關的bean到當前的上下文中,並自動掃描系統中可能需要生成api文檔那些類, 並生成相應的信息緩存起來。如果項目MVC控制層用的是springMvc那麼會自動掃描所有Controller類,並生成對應的文檔描述數據.該數據是json格式,通過路徑:項目地址/ v2/api-docs可以訪問到該數據,然後swaggerUI根據這份數據生成相應的文檔描述界面。 因為我們能拿到這份數據,所以我們也可以生成自己的頁面.

SpringBoot整合Swagger2

引入依賴

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.7.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.7.0</version>
</dependency>

注意:jdk1.8以上才能運行swagger2

編寫配置類配置Swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig{
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.example.yourproject"))//這裡填寫項目package
                .paths(PathSelectors.any())
                .build();
    }//springfox為我們提供了一個Docket(摘要的意思)類,我們需要把它做成一個Bean注入到spring中, 顯然,我們需要一個配置文件,並通過一種方式(顯然它會是一個註解)告訴程序,這是一個Swagger配置文件。
  
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2構建RESTful API")
                .description("rest api 文檔構建利器")
                .termsOfServiceUrl("//www.cnblogs.com/yrxing/")
                .contact("xing")
                .version("1.0")
                .build();
    }
  
}//springfox允許我們將信息組合成一個ApiInfo的類,作為構造參數傳給Docket

在這裡插入圖片描述

訪問://localhost:{your_server_port}/swagger-ui.html

Swagger2常用註解使用

在這裡插入圖片描述

@Api()、@ApiOperation()

@RestController
@RequestMapping(value = "/user", produces = APPLICATION_JSON_VALUE) //配置返回值 application/json
@Api(tags = "用戶管理")
public class HelloController {
  
    ArrayList<User> users = new ArrayList<>();
  
    @ApiOperation(value = "獲取用戶列表", notes = "獲取所有用戶信息")
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public List<User> hello() {
        users.add(new User("邏輯", "luoji"));
        users.add(new User("葉文傑", "yewenjie"));
        return users;
    }
}

@ApiModel()、@ApiModelProperty()

@ApiModel(description = "用戶",value = "用戶")
public class User {
  
    private String id;
  
  @ApiModelProperty(value = "用戶名")//value屬性指明了該字段的含義(描述 Description)
    private String username;
  
   @ApiModelProperty(hidden = true)//此註解可以作用在字段或者方法上,只要 hidden 屬性為 true ,該字段或者方法就不會被生成api文檔.
    private String password;
  
    private String email;
  
    private Integer age;
  
    private Boolean enabled;
  
}

@ApiParam()

@ApiOperation(value = "獲取用戶詳細信息", notes = "根據url的id來獲取用戶詳細信息")
 @RequestMapping(value = "getUser/{id}", method = RequestMethod.GET)
 
 public User getUser(@ApiParam(naeme = "id",value = "用戶id", required = true)  @PathVariable(value = "id") String id) {
     return new User(id, "itguang", "123456");
 }//@ApiParam這個註解,需要注意的是,這個註解方法的參數前面,不能直接用在方法上面. 

@ApiImplicitParams()、@ApiImplicitparam()

    ···
  @Api("測試用例1")
  @Controller
  public class swaggerTestUse(){
      @ApiOperation(value = "apiOperationSwaggerTest", notes = "apiOperationSwagger測試")
      @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id入參", required = true, dataType = "Integer", paramType = "query"),
                        @ApiImplicitParam(name = "brand", value = "brand", required = true, dataType = "BRAND", paramType = "body")
    })
      public void apiOperationSwaggerTest(Integer id, Brand band){
      }
  }

參考鏈接:

//www.cnblogs.com/shianliang/p/10472622.html

//blog.csdn.net/qq_44543508/article/details/105381058

Tags: