先後端分離的項目須要先後端開發人員協同工做,後臺開發人員須要給到前端開發者一套API文檔;利用Swagger能夠簡單高效的幫助後臺開發者生成RestfulAPI開發文檔html
<!-- 自動生成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>
在SpringBoot項目的啓動類上標註 @EnableSwagger2 使項目支持 swagger 註解java
在須要生成API文檔的地方標註相應註解便可git
》方法級別github
@ApiOperation(value = "查詢全部用戶信息")spring
》參數級別(單個參數)apache
@ApiParam(value = "訂單ID") 後端
》參數級別(參數是一個實體類)服務器
@ApiModelProperty(value = "用戶ID")restful
技巧01:直接在實體類中的某個字段上添加 @ApiModelProperty(value = "用戶ID")
技巧01:若是項目設置了 上下文路徑,那麼就須要在前面添加 上下文路徑,例如
http://127.0.0.1:9999/dev/swagger-ui.html
前端開發人員須要一些後天的模擬數據,後臺開發人員能夠利用WireMock模擬一些數據供前端人員調用
技巧01:WireMock 是一個單獨的服務器
直接將數據放到指定文件,在進行一些配置後再啓動WireMock服務器就能夠啦
技巧01:這種方法使用簡單可是對於後臺開發人員不太方便,詳細使用方法請參見百度
技巧02:這種方法適合前端人員使用(PS:前端人員拿到了後臺給他的JSON文件)
到WireMock的官網把jar包下載到本地
下載的WireMock就至關於一個項目的jar包,咱們只須要在JVM上運行這個jar包便可
技巧01:進入到WireMock的jar包所在的文件夾,而後運行這個jar包
技巧02:在運行這個jar包時能夠指定端口等信息,詳情參見官方文檔
java -jar wiremock-standalone-2.17.0.jar --port=8062
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) // 響應狀態碼 ) ); } }
2.2.3.3 請求WireMock中的模擬數據
技巧01:IP地址、端口、請求路徑都是WireMock的,不是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"
}
》工具類
》》 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) ) ); } }
》》執行main方法進行消息發佈
2.2.4.3 請求WireMock中的模擬數據