1.Swagger簡介
swagger官網地址:
https://swagger.io/
swagger官網文檔介紹地址:
https://swagger.io/about/
swagge是一個易於使用的API團隊協作開發的工作,能用於查看API的生命周期,設計文檔和測試開發.因此我們在前后端分離的項目用到Swagge測試和參數獲取是再合適不過的了.
2.相關注解的介紹
1、@Api:用在請求的類上,說明該類的作用
tags="說明該類的作用"
value="該參數沒什么意義,所以不需要配置"
示例:
@Api(tags="APP用戶注冊Controller")
2、@ApiOperation:用在請求的方法上,說明方法的作用
@ApiOperation:"用在請求的方法上,說明方法的作用"
value="說明方法的作用"
notes="方法的備注說明"
示例:
@ApiOperation(value="用戶注冊",notes="手機號、密碼都是必輸項,年齡隨邊填,但必須是數字")
3、@ApiImplicitParams:用在請求的方法上,包含一組參數說明
@ApiImplicitParams:用在請求的方法上,包含一組參數說明
@ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一個請求參數的配置信息
name:參數名
value:參數的漢字說明、解釋
required:參數是否必須傳
paramType:參數放在哪個地方
· header --> 請求參數的獲取:@RequestHeader
· query --> 請求參數的獲取:@RequestParam
· path(用於restful接口)--> 請求參數的獲取:@PathVariable
· body(不常用)
· form(不常用)
dataType:參數類型,默認String,其它值dataType="Integer"
defaultValue:參數的默認值
示列:
@ApiImplicitParams({
@ApiImplicitParam(name="mobile",value="手機號",required=true,paramType="form"),
@ApiImplicitParam(name="password",value="密碼",required=true,paramType="form"),
@ApiImplicitParam(name="age",value="年齡",required=true,paramType="form",dataType="Integer")
})
4、@ApiResponses:用於請求的方法上,表示一組響應
@ApiResponses:用於請求的方法上,表示一組響應
@ApiResponse:用在@ApiResponses中,一般用於表達一個錯誤的響應信息
code:數字,例如400
message:信息,例如"請求參數沒填好"
response:拋出異常的類
示例:
@ApiOperation(value = "select1請求",notes = "多個參數,多種的查詢參數類型")
@ApiResponses({
@ApiResponse(code=400,message="請求參數沒填好"),
@ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對")
})
5、@ApiModel:用於響應類上,表示一個返回響應數據的信息
@ApiModel:用於響應類上,表示一個返回響應數據的信息
(這種一般用在post創建的時候,使用@RequestBody這樣的場景,
請求參數無法使用@ApiImplicitParam注解進行描述的時候)
@ApiModelProperty:用在屬性上,描述響應類的屬性
示例:
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(description= "返回響應數據")
public class RestMessage implements Serializable{
@ApiModelProperty(value = "是否成功")
private boolean success=true;
@ApiModelProperty(value = "返回對象")
private Object data;
@ApiModelProperty(value = "錯誤編號")
private Integer errCode;
@ApiModelProperty(value = "錯誤信息")
private String message;
/* getter/setter */
3.項目Demo
建立一個Springboot的Maven項目,然后引入如下依賴:
<!--swagger的依賴-->
<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>
啟動類中的代碼如下:
import springfox.documentation.service.Contact;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@EnableSwagger2
@SpringBootApplication
public class AfirstApplication {
public static void main(String[] args) {
SpringApplication.run(AfirstApplication.class);
}
@Bean
public Docket initApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(true)
.apiInfo(demoApiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.build();
}
private ApiInfo demoApiInfo() {
Contact contact = new Contact("xxxx","http://xxx","xxxx");
return new ApiInfoBuilder()
.title("測試API")
.description("REST風格API")
.termsOfServiceUrl("http:xxx.xx.com")
.contact(contact)
.version("1.0")
.build();
}
}
TestController:
@RestController
@RequestMapping(path = "/test")
@Api(tags = {"test"})
public class TestController {
@GetMapping("/swagger")
@ApiOperation(value = "測試")
public String test() {
return "testok";
}
}
訪問:http://localhost:8080/swagger-ui.html