1. 程式人生 > >springboot 整合 swagger 介面文件

springboot 整合 swagger 介面文件

優缺點:

    優點:省去額外的工作量 單獨去維護一套介面文件、配置簡單(僅使用幾個註解即可完成介面文件的編寫)、支援線上測試

    缺點:額外的工作量(對於程式設計師來說)

>>step one:新增依賴

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>

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

>>step two:controller 添加註解

@Api(tags = "基礎兌換幣 相關介面")
@RestController
@RequestMapping(value = "/web-api/v1/base/pair", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class BasePairController {

    @Autowired
    private BasePairService basePairService;

    //分頁列表
    @ApiOperation(value = "基礎兌換幣 列表", notes = "根據交易所ID獲取 基礎兌換幣。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market_id", value = "交易所ID | Long", required = true, paramType = "query", defaultValue = "4")
    })
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public CommandResult<List<BasePairVO>> listByMarketId(@RequestParam(value = "market_id", defaultValue = "0") Long marketId) {
        if (marketId == 0){
            return CommandResult.ofFail("引數:交易所ID 為空");
        }
        try {
            return basePairService.listByMarketId(marketId);
        }catch (Exception e){
            log.error("【獲取 基礎兌換幣 列表】請求異常", e);
            return CommandResult.ofFail("請求異常");
        }
    }

}

說明:

@Api:使用在 controller上,表明該控制器的作用(用來做什麼)

@ApiOperation:使用在具體的方法上,表明該方法的作用(用來做什麼)

@ApiImplicitParams:多引數說明

@ApiImplicitParam:單引數說明

>>step three:返回的物件添加註解

@ApiModel(description = "交易所下基礎兌換幣 列表資訊")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class BasePairVO {

    @ApiModelProperty(value = "基礎兌換幣ID", position = 1)
    @JsonProperty("base_pair_id")
    private Long basePairId;

    @ApiModelProperty(value = "交易所ID", position = 2)
    @JsonProperty("market_id")
    private Long marketId;

    @ApiModelProperty(value = "交易所的基礎兌換幣", position = 3)
    @JsonProperty("pair_name")
    private String pairName;

    public static BasePairVO doToVo(TBasePairDO pairDO){
        if (pairDO == null){
            return null;
        }
        return new BasePairVO(pairDO.getAutoId(), pairDO.getMarketId(), pairDO.getPairName().toUpperCase());
    }
}

說明:

@ApiModel:用在返回物件上。

@ApiModelProperty:對返回物件的描述。

>>step four: 加配置

@Configuration
public class Swagger2DevConfig {

    @Profile({"default", "pro"})
    @Bean
    public Docket createWebApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .enable(false)
            .select().build();
    }

    @Profile("dev")
    @Bean
    public Docket createWebApiDev() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoDev())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    @Profile("test")
    @Bean
    public Docket createWebApiTest() {
        return new Docket(DocumentationType.SWAGGER_2)
                .host("test.echo.com/demo")
                .protocols(Sets.newHashSet("https"))
                .apiInfo(apiInfoDev())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfoDev() {
        return new ApiInfoBuilder()
                .title("市值API")
                .description("市值api介面文件\n" +
                        "\n" +
                        "測試環境:https://test.echo.com/demo\n" +
                        "生產環境\thttps://test.echo.com/demo\n")
                .contact(new Contact("echo", "", ""))
                .version("1.0")
                .build();
    }

}