1.介绍swagger2
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
2.引入Swagger2和swagger ui
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3.编写配置类
package com.offcn.user.config;
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 AppSwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.offcn.user.controller")) // swagger2 测试类
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("七易众筹-系统平台接口文档") //大标题
.description("提供用户模块的文档") //提示信息
.termsOfServiceUrl("http://www.ujiuye.com/")
.version("1.0") //版本
.build();
}
}
4.使用注解
使用Swagger注解
@Api(tags="")
用在请求的类上,表示对类的说明
tags"说明该类的作用,可以在UI界面上看到的注解"
@ApiOperation(value="")
用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
@ApiImplicitParams
用在请求的方法上,表示一组参数说明
@ApiImplicitParam:
指定一个请求参数的各个方面
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
主要有两种用途:
用于响应类上,表示一个返回响应数据的信息
入参实体:使用@RequestBody这样的场景, 请求参数无法使用
@ApiImplicitParam注解进行描述的时候
@ApiModelProperty
用在属性上,描述响应类的属性
5.编写pojo文件
package com.offcn.user.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("测试实体")
public class User {
@ApiModelProperty(value = "主键")
private int id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "电子邮件")
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
6.编写测试类
package com.offcn.user.controller;
import com.offcn.user.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "第一个Swagger测试")
@RestController
public class HelloController {
@ApiOperation("测试方法hello")
@ApiImplicitParams(value={
@ApiImplicitParam(name="name",value="姓名",required = true),@ApiImplicitParam(name="age",value="年龄")
})
@GetMapping("/hello")
public String hello(String name){
return "OK:"+name;
}
@ApiOperation("保存用户")
@ApiImplicitParams(value={
@ApiImplicitParam(name="name",value="姓名",required = true),@ApiImplicitParam(name="email",value="电子邮件")
})
@PostMapping("/user")
public User save(String name, String email){
User user = new User();
user.setName(name);
user.setEmail(email);
return user;
}
}
http://localhost:7000/swagger-ui.html
7.主配置类添加注解
@EnableSwagger2 //开启swagger2