spring-boot-route(五)整合Swagger生成接口文檔


目前,大多數公司都采用了前后端分離的開發模式,為了解決前后端人員的溝通問題,后端人員在開發接口的時候會選擇使用swagger2來生成對應的接口文檔,swagger2提供了強大的頁面調試功能,這樣可以有效解決前后端人員溝通難的問題。

下面我們使用SpringBoot結合swagger2生成Restful API文檔。

一 搭建項目,引入依賴

新建一個spring-boot-swaager的項目,引入swaager2的依賴,由於swagger2的ui不是很美觀,這里將使用開源的swagger-bootstrap-ui做為ui。

引入依賴

<!-- swaager2依賴 -->    
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- swaager2ui -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

項目中配置swagger相關信息

@Configuration
@EnableSwagger2
public class configuration {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.javatrip.swagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                // 標題
                .title("某某項目接口文檔")
                // 描述
                .description("swagger2接口文檔使用演示")
                // 版本
                .version("1.0")
                // 許可證
                .license("MIT")
                // 許可證地址
                .licenseUrl("www.xx.com")
                // 服務端地址
                .termsOfServiceUrl("https://www.cnblogs.com/zhixie/")
                // 聯系信息
                .contact(new Contact("java旅途","https://www.cnblogs.com/zhixie/","binzh303@163.com"))
                .build();
    }
}

訪問路徑,查看生成效果

文章中使用的這個ui,接口文檔地址為ip:port/doc.html,生成的文檔信息如下:

二 編寫Restful接口

新建實體類

@ApiModel("用戶實體類")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
    @ApiModelProperty("姓名")
    private String name;
    @ApiModelProperty(value = "年齡")
    private int age;
}

新建Restful接口

@Api(tags = "用戶接口")
@RestController
@RequestMapping("person")
public class PersonController {

    @ApiOperation(value = "獲取用戶列表",notes = "根據name獲取用戶列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用戶姓名",dataType = "String",required = true),
            @ApiImplicitParam(name = "age",value = "年齡",dataType = "int",required = true)
    })
    @GetMapping("/{name}")
    public Person getPerson(@PathVariable("name") String name,@RequestParam int age){
        return new Person(name,age);
    }

    @ApiOperation(value = "新增用戶",notes = "根據用戶實體類新增用戶")
    @ApiImplicitParam(name = "person",value = "用戶實體類",dataType = "Person",required = true)
    @PostMapping("add")
    public int addPerson(@RequestBody Person person){
        if(StringUtils.isEmpty(person)){
            return -1;
        }
        return 1;
    }

    @ApiOperation(value = "更新用戶信息",notes = "根據用戶實體更新用戶信息")
    @ApiImplicitParam(name = "person",value = "用戶實體類",dataType = "Person",required = true)
    @PutMapping("update")
    public int updatePerson(@RequestBody Person person){
        if(StringUtils.isEmpty(person)){
            return -1;
        }
        return 1;
    }

    @ApiOperation(value = "刪除用戶信息",notes = "根據用戶名刪除用戶信息")
    @ApiImplicitParam(name = "name",value = "用戶姓名",dataType = "String",required = true)
    @DeleteMapping("/{name}")
    public int deletePerson(@PathVariable(name = "name") String name){
        if(StringUtils.isEmpty(name)){
            return -1;
        }
        return 1;
    }
}

三 swagger文檔簡介

我就直接用圖來表示了,這樣看着也更加直觀

swagger2注解對應到文檔上的表現形式如上。swagger2支持在線調試,打開某個具體的接口,根據提示填寫對應的參數,點擊發送就可返回響應結果。


本文示例代碼已上傳至github,點個star支持一下!

Spring Boot系列教程目錄

spring-boot-route(一)Controller接收參數的幾種方式

spring-boot-route(二)讀取配置文件的幾種方式

spring-boot-route(三)實現多文件上傳

spring-boot-route(四)全局異常處理

spring-boot-route(五)整合swagger生成接口文檔

spring-boot-route(六)整合JApiDocs生成接口文檔

spring-boot-route(七)整合jdbcTemplate操作數據庫

spring-boot-route(八)整合mybatis操作數據庫

spring-boot-route(九)整合JPA操作數據庫

spring-boot-route(十)多數據源切換

spring-boot-route(十一)數據庫配置信息加密

spring-boot-route(十二)整合redis做為緩存

spring-boot-route(十三)整合RabbitMQ

spring-boot-route(十四)整合Kafka

spring-boot-route(十五)整合RocketMQ

spring-boot-route(十六)使用logback生產日志文件

spring-boot-route(十七)使用aop記錄操作日志

spring-boot-route(十八)spring-boot-adtuator監控應用

spring-boot-route(十九)spring-boot-admin監控服務

spring-boot-route(二十)Spring Task實現簡單定時任務

spring-boot-route(二十一)quartz實現動態定時任務

spring-boot-route(二十二)實現郵件發送功能

spring-boot-route(二十三)開發微信公眾號

spring-boot-route(二十四)分布式session的一致性處理

spring-boot-route(二十五)兩行代碼實現國際化

spring-boot-route(二十六)整合webSocket

這個系列的文章都是工作中頻繁用到的知識,學完這個系列,應付日常開發綽綽有余。如果還想了解其他內容,掃面下方二維碼告訴我,我會進一步完善這個系列的文章!


免責聲明!

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



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