1.导入相应的maven依赖
<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>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.创建springboot的yml配置文件
(1)application.yml文件如下
spring:
profiles:
active: dev
(2)application-dev.yml文件如下
server:
port: 8877
(3)application-pro.yml文件如下
server:
port: 8899
3.创建config目录,并且创建Swagger的配置类
package com.lz.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;
import java.util.ArrayList;
import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;
@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
// 配置了Swagger的Docket的bean的实例
@Bean
public Docket docket(Environment environment){
// 设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
// 获取项目的环境
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("科比")
// 是否启用Swagger,如果值为false,则不能在浏览器访问swagger-ui.html页面
.enable(flag)
.select()
// RequestHandlerSelectors:配置要扫描接口的方式
// basePackage:指定要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.lz.controller"))
// any():扫面全部
// .apis(RequestHandlerSelectors.any())
// none():不扫描
// .apis(RequestHandlerSelectors.none())
// withClassAnnotation():扫描类上的注解,参数是一个注解的反射对象
// .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
// withMethodAnnotation():扫描方法上的注解
// .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
// 过滤什么路径
// .paths(PathSelectors.ant("/com/**"))
.build();
}
// Swagger信息apiInfo
private ApiInfo apiInfo(){
// 作者信息
Contact contact = new Contact("kobe", "https://www.baidu.com", "147258369@qq.com");
return new ApiInfo(
"SwaggerAPI文档",
"认真把握每一天",
"1.0",
"https://www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
4.输入访问的端口:http://localhost:8877/swagger-ui.html
然后就可以看到swagger的配置信息
4.创建实体类,对实体类,以及实体类中的属性进行说明
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
5.创建controller,进行接口的测试
import com.lz.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping(value = "/hello")
public String hello(){
return "hello";
}
// 返回值中存在实体类,就会被扫描到swagger中
@PostMapping(value = "/user")
public User user(){
return new User();
}
@ApiOperation("hello1控制类")
@GetMapping(value = "/hello1")
public String hello1(@ApiParam("用户名") String username){
return "hello1" + username;
}
@ApiOperation("post控制类")
@PostMapping(value = "/post")
public User hello2(@ApiParam("用户") User user){
return user;
}
}
6.在swagger-ui.html进行接口请求测试