SpringBoot18 Swagger、API接口文檔生成、WireMock、模擬後臺數據
1 Swagger
1.1 簡述
前後端分離的項目需要前後端開發人員協同工作,後臺開發人員需要給到前端開發者一套API文檔;利用Swagger可以簡單高效的幫助後臺開發者生成RestfulAPI開發文檔
官網地址:點擊前往
1.2 使用步驟
1.2.1 引入swagger相關依賴
<!-- 自動生成restfulAPI文檔相關 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
1.2.3 支持註解
在SpringBoot項目的啟動類上標註 @EnableSwagger2 使項目支持 swagger 註解
1.2.4 文檔註解
在需要生成API文檔的地方標註相應註解即可
》方法級別
@ApiOperation(value = "查詢所有用戶信息")
》參數級別(單個參數)
@ApiParam(value = "訂單ID")
》參數級別(參數是一個實體類)
@ApiModelProperty(value = "用戶ID")
技巧01:直接在實體類中的某個字段上添加 @ApiModelProperty(value = "用戶ID")
1.2.5 訪問 swagger-ui.html
技巧01:如果項目設置了 上下文路徑,那麽就需要在前面添加 上下文路徑,例如
http://127.0.0.1:9999/dev/swagger-ui.html
2 WireMock
前端開發人員需要一些後天的模擬數據,後臺開發人員可以利用WireMock模擬一些數據供前端人員調用
技巧01:WireMock 是一個單獨的服務器
2.1 單獨使用WireMock
直接將數據放到指定文件,在進行一些配置後再啟動WireMock服務器就可以啦
技巧01:這種方法使用簡單但是對於後臺開發人員不太方便,詳細使用方法請參見百度
技巧02:這種方法適合前端人員使用(PS:前端人員拿到了後臺給他的JSON文件)
2.2 在項目中集成WireMock
2.2.1 下載WireMock的jar包
到WireMock的官網把jar包下載到本地
WierMock官網:點擊前往
2.2.2 啟動WierMock
下載的WireMock就相當於一個項目的jar包,我們只需要在JVM上運行這個jar包即可
技巧01:進入到WireMock的jar包所在的文件夾,然後運行這個jar包
技巧02:在運行這個jar包時可以指定端口等信息,詳情參見官方文檔
java -jar wiremock-standalone-2.17.0.jar --port=8062
2.2.3 在SpringBoot項目中集成(簡單)
2.2.3.1 下載先關jar包
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>2.14.0</version> </dependency>
2.2.3.2 發布信息
》連接配置
技巧01:需要配置WireMock的IP地址以及端口,如果是本地就不需要配置IP地址,直接配置端口即可
》清空上一次的發布信息
》執行main方法進行消息發布
package com.example.wiremock.util; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.core.io.ClassPathResource; import java.io.IOException; import static com.github.tomakehurst.wiremock.client.WireMock.*; /** * @author 王楊帥 * @create 2018-05-11 9:29 * @desc **/ public class WireMockServerUtil02 { public static void main(String[] args) throws IOException { // 01 連接配置 configureFor(8062); // 配置連接信息(PS:這個端口必須和啟動WireMock的端口保持一致) // 02 清空發布信息 removeAllMappings(); // 清空上一次的發布信息 // 03 發布新信息 stubFor( get(urlPathEqualTo("/wiremock/test")) // 設置請求路徑 .willReturn( aResponse() // 設置響應信息 .withBody("{\"id\":12,\"name\":null,\"password\":null}") // 響應數據 .withStatus(200) // 響應狀態碼 ) ); } }WireMockServerUtil02.java
2.2.3.3 請求WireMock中的模擬數據
技巧01:IP地址、端口、請求路徑都是WireMock的,不是SpringBoot項目的
2.2.4 在SpringBoot項目中集成(實用)
將需要發布的數據放到一個txt文件中去,需要發布某個txt文件中的數據時直接調用某個方法即可
2.2.4.1 下載相關jar包
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>2.14.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2.2.4.2 發布信息
》在resources目錄下創建一個文件夾
》在裏面創建txt文件來存放你需要發布的後臺模擬數據(PS:數據要以JSON格式書寫)
技巧01:一個文件只能存放一個請求對應的後臺模擬數據
{
"name": "王楊帥",
"age": 24,
"address": "chongqingyuzu"
"gender": "F"
}
user.txt
》工具類
》》 WireMock連接信息配置
》》 清空發布信息
》》 發布工具方法
public static void mock(String filename, String url) throws IOException { ClassPathResource classPathResource = new ClassPathResource("mock/response/" + filename); String data = StringUtils.join(FileUtils.readLines(classPathResource.getFile(), "UTF-8").toArray(), "\n"); stubFor( get(urlPathEqualTo(url)) .willReturn( aResponse() .withBody(data) .withStatus(200) ) ); }發布方法
》》調用發布方法發布信息
技巧01:只需要傳入 文件名 和 請求路徑即可
package com.example.wiremock.util; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.core.io.ClassPathResource; import java.io.IOException; import static com.github.tomakehurst.wiremock.client.WireMock.*; /** * @author 王楊帥 * @create 2018-05-11 9:29 * @desc **/ public class WireMockServerUtil { public static void main(String[] args) throws IOException { configureFor(8062); removeAllMappings(); mock("user.txt", "/user"); mock("teacher.txt", "/teacher"); } public static void mock(String filename, String url) throws IOException { ClassPathResource classPathResource = new ClassPathResource("mock/response/" + filename); String data = StringUtils.join(FileUtils.readLines(classPathResource.getFile(), "UTF-8").toArray(), "\n"); stubFor( get(urlPathEqualTo(url)) .willReturn( aResponse() .withBody(data) .withStatus(200) ) ); } }WireMockServerUtil.java
》》執行main方法進行消息發布
2.2.4.3 請求WireMock中的模擬數據
SpringBoot18 Swagger、API接口文檔生成、WireMock、模擬後臺數據