swagger官网地址:
https://swagger.io/
swagger官网文档介绍地址:
https://swagger.io/about/
swagge是一个易于使用的API团队协作开发的工作,能用于查看API的生命周期,设计文档和测试开发.因此我们在前后端分离的项目用到Swagge测试和参数获取是再合适不过的了.
image.png
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 */
建立一个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
image.png