1.创建SpringBoot项目swagger2
2.在项目的pom文件中引入相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>swagger2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>swagger2</name>
<description>swagger2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.在项目中创建实体类
package com.cloud.eneity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
@Data
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description="员工实体")
public class User {
@ApiModelProperty(value="员工姓名")
private String name;
@ApiModelProperty(value="员工年龄")
private Integer age;
@ApiModelProperty(value="员工住址")
private String addr;
}
package com.cloud.eneity;
public class Contact{
private String name;
private String url;
private String mail;
public Contact(String name, String url, String mail) {
this.name = name;
this.url = url;
this.mail = mail;
}
}
4.在项目中,创建controller类
package com.cloud.controller;
import com.cloud.eneity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/admin")
@Api(description="测试类")
public class DemoController {
@RequestMapping("getUser")
@ApiOperation("方法描述")
public User getUser(@ApiParam("参数描述") User u){
User user = new User();
user.setName(u.getName());
user.setAge(u.getAge());
user.setAddr(u.getAddr());
return user;
}
}
5.在项目中,创建Swagger的配置类
创建包com.cloud.config,在包里创建配置类Swagger2Config。
package com.cloud.config;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@Component
public class Swagger2Config {
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
.build();
}
//三、Swagger2常用配置和注解
//1.对文档的整体添加页面标题、描述信息、创建人等信息
@Bean
public ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
//页面标题
.title("后台管理系统-API文档")
//描述信息
.description("本文档描述了后台管理系统微服务接口定义")
//版本号
.version("1.0")
//创建人
.contact(new Contact("张三","http://localhost:8080/","123456@163.com"))
.build();
}
}
6.通过地址访问测试: http://localhost:8080/swagger-ui.html
1、使用后效果如下
2.控制层常用的注解
@Api 使用位置在类上,描述介绍整个类
@ApiOperation 使用位置在方法上,用来介绍方法
@ApiParam 使用位置在参数上,用来介绍参数
package com.cloud.controller;
import com.cloud.eneity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/admin")
@Api(description="测试类")
public class DemoController {
@RequestMapping("getUser")
@ApiOperation("方法描述")
public User getUser(@ApiParam("参数描述") User u){
User user = new User();
user.setName(u.getName());
user.setAge(u.getAge());
user.setAddr(u.getAddr());
return user;
}
}
3.使用后的效果如下:
4.在modle类上常用的注解
@ApiModel使用位置在modle类上,用来介绍实体类
@ApiModelProperty 使用位置在modle类的属性上,用来介绍解释属性
package com.cloud.eneity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
@Data
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description="员工实体")
public class User {
@ApiModelProperty(value="员工姓名")
private String name;
@ApiModelProperty(value="员工年龄")
private Integer age;
@ApiModelProperty(value="员工住址")
private String addr;
}
使用后的效果如下: