分佈式微服務專欄003(springboot中集成Swagger-bootstrap-ui--postman)

歡迎大家加入我的github項目,一起學習,一起發展
---> 全棧工程師進階學習站
---> 我的前端學習筆記
--->行業內最新最羣的報告,工作日每日更新

—>原生 js 訓練計劃

SpringBoot 中集成swagger

集成swagger-ui與Bootstrap-ui

導入Postman使用

1.添加依賴

<!--        swagger依賴開始-->
<!--        主要依賴-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
<!--        官方ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
<!--        bootstrap-ui-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.3</version>
        </dependency>
<!--        swagger依賴結束-->

2.添加配置類

@Configuration    
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
    @Bean
    public Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Gavin_Swagger")
                .version("1.0")
                .build();
    }
}

3.在Controller中添加配置註解

在類上加註解

@Api(tags = "xx", produces = "xx")  //tags是分類標籤,可以作爲版本號區別

在方法上加註解

@ApiOperation("展示")
@PostMapping(value = "/show")
//這邊的形參,@valid的作用跟@requestBody一樣,但方便參數在swagger中展示成列表形式(樣式友好)
public User select(@Valid User user){

    return usersService.select();
}

4.在實體類上加註解

@ApiModel
@Data
public class User {
    private Integer uid;
  
  //這邊是加在屬性上的註解
    @ApiModelProperty(value = "姓名",required = false)
    private String username;

    private String password;

    private Integer gender;
}

5.訪問路徑

官方ui路徑

http://localhost:8080/swagger-ui.html

image-20190923192600958

bootstrap-ui路徑

http://localhost:8080/doc.html

image-20190923192617230

6.整合進Postman

1.打開官方ui路徑

image-20190923192331816

2.打開postman,左上角點import

image-20190923192435183

3.打開collections可以看到api的接口了

image-20190923192529778

7.swagger常見註解詳解

swagger,指定swagger spec版本,2.0
info,提供API的元數據
tags,補充的元數據,在swagger ui中,用於作爲api的分組標籤
host,主機,如果沒有提供,則使用文檔所在的host
basePath,相對於host的路徑
schemes,API的傳輸協議,http,https,ws,wss
consumes,API可以消費的MIME類型列表
produces,API產生的MIME類型列表
paths,API的路徑,以及每個路徑的HTTP方法,一個路徑加上一個HTTP方法構成了一個操作。每個操作都有以下內容:
tags,操作的標籤
summary,短摘要
description,描述
externalDocs,外部文檔
operationId,標識操作的唯一字符串
consumes,MIME類型列表
produces,MIME類型列表
parameters,參數列表
responses,應答狀態碼和對於的消息的Schema
schemes,傳輸協議
deprecated,不推薦使用
security,安全
definitions,定義API消費或生產的數據類型,使用json-schema描述,操作的parameter和response部分可以通過引用的方式使用definitions部分定義的schema
parameters,多個操作共用的參數
responses,多個操作共用的響應
securityDefinitions,安全scheme定義
security,安全聲明
externalDocs,附加的外部文檔
```閒聊時刻

10年短信公司,互聯網100強企業!主營業務:短信(國內和國際)App H5一鍵免密登錄,防薅羊毛接口,圖片OCR,空號檢測,活躍號檢測,人證覈驗,活體檢測,高防ddos攻擊等等200多個企業服務接口!需要的聯繫他13592366783 官方鏈接:https://zz.253.com/v5.html#/register?uid=1953

自己公司需求 偶然間 用了一家第三方接口供應商,產品應有盡有,很齊全,對接文檔非常詳細,彼此都很節約時間,主要非常穩定,包括服務方面很給力,有興趣的博友,可以聯繫他,算是對合作夥伴的支持了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章