1. 程式人生 > >SpringBoot中部署Swagger2和Swagger-UI

SpringBoot中部署Swagger2和Swagger-UI

des document obj 接口 pub name swagger lombok work

1 Gradle配置
在dependencies中添加以下依賴:

implementation("io.springfox:springfox-swagger2:2.7.0")
implementation("io.springfox:springfox-swagger-ui:2.7.0")

具體的版本可以在https://mvnrepository.com/artifact/io.springfox中查看到

2 添加Swagger2配置類

package com.learning.test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 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.EnableSwagger2; @Configuration @EnableWebMvc @EnableSwagger2 public class Swagger2Configuration implements WebMvcConfigurer { //swagger2的配置文件,這裏可以配置swagger2的一些基本的內容,比如掃描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.learning.test.controller")) .paths(PathSelectors.any()) .build(); } // 構建api文檔的詳細信息 private ApiInfo apiInfo() { return new ApiInfoBuilder() // 頁面標題 .title("API接口說明") // 創建人 .contact(new Contact("lasdaybg", "", "")) // 版本號 .version("") // 描述 .description("") .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }

3 Controller示例

package com.learning.test.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.learning.test.model.MyObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/test")
@Api("測試接口")
public class TestController {
  @RequestMapping(method = RequestMethod.GET)
  @ApiOperation(value = "查詢對象")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "param1", value = "入參1"),
      @ApiImplicitParam(name = "param2", value = "入參2")})
  public MyObject get(@RequestParam(required = false) String param1,
                      @RequestParam(required = false) String param2) {
    return new MyObject();
  }

  @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @ApiOperation(value = "創建對象")
  public void create(@RequestBody(required = false) MyObject myObject) {}
}

MyObject的類聲明如下:
package com.learning.test.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "MyObject", description = "數據模型")
public class MyObject {

  @ApiModelProperty(value = "名稱")
  private String name;

  @ApiModelProperty(value = "參數1")
  private Integer param1;

  @ApiModelProperty(value = "參數2")
  private Boolean param2;
}

這裏用到了幾類註解:
@Api
用在類上,說明這個是Swagger的資源

@ApiOperation
用在方法上,對方法功能做一個說明

@ApiImplicitParams,ApiImplicitParam
用在方法上,對方法的入參進行說明

@ApiModel
用在模型對象上,對對象的屬性等進行說明

另外,這裏用到了lombok,具體用法請自行百度。

4 查看swagger文檔

啟動程序,在瀏覽器中輸入http://localhost:8080/swagger-ui.html,即可看到swagger的文檔說明

SpringBoot中部署Swagger2和Swagger-UI