1. 程式人生 > >第九篇: 用spring boot整合swagger2建立API文件

第九篇: 用spring boot整合swagger2建立API文件

簡介:

Swagger的目標是為REST APIs 定義一個標準的,與語言無關的介面,使人和計算機在看不到原始碼或者看不到文件或者不能通過網路流量檢測的情況下能發現和理解各種服務的功能。當服務通過Swagger定義,消費者就能與遠端的服務互動通過少量的實現邏輯。類似於低階程式設計介面,Swagger去掉了呼叫服務時的很多猜測。

我覺得把Swagger當成一種開發工具就好,省去了手寫文件的步驟,生成文件並能測試,註解的方式讓其他人看程式碼也更加清楚方便。想了解更多請自己檢視官方文件!

建立工程:

其完整的pom檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wujie</groupId>
    <artifactId>springboot-swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-swagger</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

新建一個配置類:

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wujie.springbootswagger.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("springboot 整合swaager2 構建api文件")
                .description("springboot 學習 ,https://blog.csdn.net/u013783065")
                .termsOfServiceUrl("https://blog.csdn.net/u013783065")
                .version("1.0")
                .build();
    }
}

新建一個測試controller:

@Api(description = "swagger測試")
@RestController
public class HelloController {
    @ApiOperation(value = "test1",notes = "測試swagger")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value="姓名",required = true,dataType = "string"),
            @ApiImplicitParam(name = "sex",value = "性別",required = true,dataType = "string")})
    @RequestMapping(value = "test1",method = RequestMethod.POST)
    public String test1(@RequestParam String name ,@RequestParam String sex){
        return name+" : "+sex;
    }
}

最後我們還可以進行測試:

到此我們整合swagger也就算完成,我之前也在ssm上進行過整合,整體來說還是沒有springboot來的簡單輕鬆方便。

最後附上一些swagger常用的註解以及解釋:

@Api:修飾整個類,描述Controller的作用
@ApiOperation:描述一個類的一個方法,或者說一個介面
@ApiParam:單個引數描述
@ApiModel:用物件來接收引數
@ApiProperty:用物件接收引數時,描述物件的一個欄位
@ApiResponse:HTTP響應其中1個描述
@ApiResponses:HTTP響應整體描述
@ApiIgnore:使用該註解忽略這個API
@ApiError :發生錯誤返回的資訊
@ApiParamImplicitL:一個請求引數
@ApiParamsImplicit 多個請求引數

原始碼地址

歡迎關注我的公眾號我們一起學習: