swagger官网地址: https://swagger.io/ swagger官网文档介绍地址: https://swagger.io/about/
swagge是一个易于使用的API团队协做开发的工做,能用于查看API的生命周期,设计文档和测试开发.所以咱们在先后端分离的项目用到Swagge测试和参数获取是再合适不过的了.html
一、@Api:用在请求的类上,说明该类的做用
tags="说明该类的做用"
value="该参数没什么意义,因此不须要配置"
示例:java
@Api(tags="APP用户注册Controller")
二、@ApiOperation:用在请求的方法上,说明方法的做用
@ApiOperation:"用在请求的方法上,说明方法的做用"
value="说明方法的做用"
notes="方法的备注说明"
示例:web
@ApiOperation(value="用户注册",notes="手机号、密码都是必输项,年龄随边填,但必须是数字")
三、@ApiImplicitParams:用在请求的方法上,包含一组参数说明
@ApiImplicitParams:用在请求的方法上,包含一组参数说明
@ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一个请求参数的配置信息
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪一个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不经常使用)
· form(不经常使用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值spring
示列:后端
@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") })
四、@ApiResponses:用于请求的方法上,表示一组响应
@ApiResponses:用于请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,通常用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
示例:api
@ApiOperation(value = "select1请求",notes = "多个参数,多种的查询参数类型") @ApiResponses({ @ApiResponse(code=400,message="请求参数没填好"), @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对") })
五、@ApiModel:用于响应类上,表示一个返回响应数据的信息
@ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种通常用在post建立的时候,使用@RequestBody这样的场景,
请求参数没法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性restful
示例:app
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>
启动类中的代码以下:post
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