springboot - 3整合swagger-ui
2019-09-30 本文已影响0人
lkd_wang
Swagger 是一款RESTFUL接口的、基于YAML、JSON语言的文档在线自动生成、代码自动生成的工具。
官网地址:https://swagger.io/
pom依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springBoot</groupId>
<artifactId>springBoot</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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.6.1</version>
</dependency>
</dependencies>
<build>
<finalName>springBoot</finalName>
</build>
</project>
添加swagger初始化类
1569826506(1).png
类容
package com.wyl.boot;
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 Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.wyl.boot"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("wyl的boot")
.version("1.0")
.build();
}
}
创建一个swagger 的测试类
1569826851(1).png
类容如下
package com.wyl.boot.swagger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/swagger")
public class SwaggerController {
@ApiOperation(value = "第一个测试")
@ApiImplicitParam(name = "data", value = "json数据", required = true, dataType = "String")
@RequestMapping(value = "/firstTest", method = RequestMethod.POST)
public Object firstTest(@RequestBody String data) {
return "第一个测试成功";
}
}
访问路径:http://127.0.0.1:8089/springBoot/swagger-ui.html#!
我们试一下用实体类接收参数:
创建2个实体类:
1569834797(1).png
代码如下:
package com.wyl.boot.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel
public class Result<T> {
@ApiModelProperty(value = "0")
private int code;
@ApiModelProperty(value = "true")
private boolean status;
@ApiModelProperty(value = "成功")
private String remark;
@ApiModelProperty(value = "返回值")
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Result(int code,boolean status,String remark,T data){
this.code = code;
this.status = status;
this.remark = remark;
this.data = data;
}
public static <T> Result<T> success (T data) {
return new Result<T>(0,true,"",data);
}
}
package com.wyl.boot.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel
public class UserDo {
@ApiModelProperty(value = "主键")
private String id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "手机号")
private String phone;
@ApiModelProperty(value = "地址")
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
[图片上传中...(1569835284(1).png-e2f10b-1569835305014-0)]
在 SwaggerController 类中添加一段
@ApiOperation(value = "第二个测试" , notes="一个复杂的测试")
@ApiImplicitParam(name = "data", value = "用户实体数据" ,required = true, dataType = "UserDo")
@RequestMapping(value = "/secondTest", method = RequestMethod.POST)
public Result<UserDo> secondTest(@RequestBody UserDo data) {
Result<UserDo> result = new Result<UserDo>(1, true, "", data);
return result;
}
重启下访问试试
image.png
1569835347(1).png
一般都能看懂的,可以联调了。