springBoot接口api swagger2显示及测试
引入依赖
org.springframework.boot
spring-boot-starter-parent
1.5.22.RELEASE
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
一,配置swagger
//显示bean的包,替换为自己的包路径
swigger2.basePackage=com.liu.controller
swigger2.title=User APIs
//接口
swigger2.description=\u63A5\u53E3˜Ž
swigger2.termsOfServiceUrl=sss
swigger2.contact=创建人
swigger2.version=1.0
swigger2.license= No License
swigger2.licenseUrl=
配置bean
@Configuration
@EnableSwagger2
public class Swigger2 {
@Value("${swigger2.title}")
private String title;
@Value("${swigger2.description}")
private String description;
@Value("${swigger2.termsOfServiceUrl}")
private String termsOfServiceUrl;
@Value("${swigger2.contact}")
private String contact;
@Value("${swigger2.version}")
private String version;
@Value("${swigger2.basePackage}")
private String basePackage;
@Value("${swigger2.license}")
private String license;
@Value("${swigger2.licenseUrl}")
private String licenseUrl;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
//apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
.apiInfo(apiInfo())
//select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,
//本例采用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,
//并产生文档内容(除了被@ApiIgnore指定的请求)。
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.termsOfServiceUrl(termsOfServiceUrl)
.contact(contact)
.version(version)
.license(license)
.licenseUrl(licenseUrl)
.build();
}
}
二,创建类
1,请求包装类
public class UserRequest implements Serializable{
private static final long serialVersionUID = -9069516241035890190L;
@ApiModelProperty(value = “用户id”)
private String uid;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
2,响应包装类
public class UserResponse implements Serializable {
private static final long serialVersionUID = -80811592907692327L;
@ApiModelProperty(value = “用户昵称”, required = true)
private String nickname;
@ApiModelProperty(value = "用户性别 ", required = true)
private String sex;
@ApiModelProperty(value = "用户余额", required = true)
private String balance;
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
}
3,实体类
@ApiModel(description= “响应数据”)
public class User {
@ApiModelProperty(value = “用户id”)
private Integer uid;
@ApiModelProperty(value = “用户昵称”,required = true)
private String nickname;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public User(Integer uid, String nickname) {
this.uid = uid;
this.nickname = nickname;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", nickname='" + nickname + '\'' +
'}';
}
}
控制器类
@Api(“用户模块”)
@RestController
@RequestMapping("/users")
public class UserController {
@ApiOperation(value = "创建用户", notes = "")
@ApiImplicitParam(name = "user", value = "请求内容", paramType = "body", required = true, dataType = "User")
@RequestMapping(method = RequestMethod.POST)
public String addUser(@ModelAttribute User user) {
System.out.println(user.toString());
System.out.println("保存用户");
//业务逻辑
return "ok";
}
@ApiOperation(value = "查询用户详情", notes = "根据id查询用户详情")
@ApiImplicitParam(name = "request", value = "请求实体", paramType = "body", required = true, dataType = "UserRequest")
@RequestMapping(value = "/getUser", method = RequestMethod.POST)
public UserResponse getUser(@RequestBody UserRequest request) {
System.out.println(request.toString());
UserResponse response = new UserResponse();
response.setBalance("100.00");
response.setNickname("aaa");
response.setSex("男");
return response;
}
@ApiOperation(value = "查询用户")
@ApiImplicitParam(name = "uid", value = "用户id", paramType = "path", required = true, dataType = "string")
@RequestMapping(value = "/{uid}", method = RequestMethod.GET)
public User getById(@PathVariable String uid) {
System.out.println(uid);
User user = new User();
user.setNickname("ccc");
user.setUid(2);
return user;
}
}
入口类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动项目
访问链接
http://localhost:8080/swagger-ui.html
显示页面
可根据页面提示的所必须字符操作