(二期)7、swagger2與postman
@Api:用在請求的類上,表示對類的說明
tags="說明該類的作用,可以在UI界面上看到的注解"
value="該參數沒什么意義,在UI界面上也看到,所以不需要配置"
@ApiOperation:用在請求的方法上,說明方法的用途、作用
value="說明方法的用途、作用"
notes="方法的備注說明"
@ApiImplicitParams:用在請求的方法上,表示一組參數說明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面
name:參數名
value:參數的漢字說明、解釋
required:參數是否必須傳
paramType:參數放在哪個地方
· header --> 請求參數的獲取:@RequestHeader
· query --> 請求參數的獲取:@RequestParam
· path(用於restful接口)--> 請求參數的獲取:@PathVariable
· body(不常用)
· form(不常用)
dataType:參數類型,默認String,其它值dataType="Integer"
defaultValue:參數的默認值
@ApiResponses:用在請求的方法上,表示一組響應
@ApiResponse:用在@ApiResponses中,一般用於表達一個錯誤的響應信息
code:數字,例如400
message:信息,例如"請求參數沒填好"
response:拋出異常的類
@ApiModel:用於響應類上,表示一個返回響應數據的信息
(這種一般用在post創建的時候,使用@RequestBody這樣的場景,
請求參數無法使用@ApiImplicitParam注解進行描述的時候)
@ApiModelProperty:用在屬性上,描述響應類的屬性
第一步:導入pom
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!-- END Swagger -->
第二步、SwaggerConfig.java中配置接口文檔基本信息和啟動swagger2支持
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("hello"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2構建RESTful APIs")
.description("歡迎來到java思維導圖社區學習")
.termsOfServiceUrl("http://www.java-mindmap.com/")
.version("1.0")
.build();
}
}
@RequestBody
1、REST 是面向資源的,這個概念非常重要,而資源是通過 URI 進行暴露。
GET /rest/api/getDogs --> GET /rest/api/dogs 獲取所有小狗狗
GET /rest/api/addDogs --> POST /rest/api/dogs 添加一個小狗狗
GET /rest/api/editDogs/:dog_id --> PUT /rest/api/dogs/:dog_id 修改一個小狗狗
GET /rest/api/deleteDogs/:dog_id --> DELETE /rest/api/dogs/:dog_id 刪除一個小狗狗
2、REST很好地利用了HTTP本身就有的一些特征,如HTTP動詞、HTTP狀態碼、HTTP報頭等等。
GET 獲取一個資源
POST 添加一個資源
PUT 修改一個資源
DELETE 刪除一個資源
200 OK
400 Bad Request
500 Internal Server Error
Authorization 認證報頭
Cache-Control 緩存報頭
Cnotent-Type 消息體類型報頭
......
- 導入
- 新建文件夾
- 保存請求
- 下載
新建項目
新建用例
添加請求信息
Post請求參數示例:
預處理和結果檢查
全局變量與環境變量
導出用例為代碼
批量執行用例