1.项目构成
2.pom.xml(3.00版本swagger-ui.html无法打开,我降级了版本)
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.swagger</groupId>
<artifactId>testswagger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testswagger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.准备工作Config层、Controller层、User层、Pojo层
Config:
package com.testswagger.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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.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;
@Configuration
@EnableSwagger2//开启swagger
public class SwaggerConfig {
//配置swagger的Docket的bean实例,以及swagger分组,即配置多个Docket
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket getDocket(Environment environment){//使用enviroment 获取当前环境,如果是特定环境开启swagger,否则不开启
Profiles profile=Profiles.of("dev","test");
System.out.println(profile);
boolean flag=environment.acceptsProfiles(profile);
System.out.println(flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //配置apiInfo
.enable(flag)
.groupName("测一测")
.select() //配置需要扫描的接口
//any:所有接口和类
//.apis(RequestHandlerSelectors.basePackage("com.testswagger.Controller")) config下的所有类
//withClassAnnotation(RestController.class) 所有带有restController的类或接口
//.withMethodAnnotation(GetMapping.class) 所有method=get的类或接口
.apis(RequestHandlerSelectors.basePackage("com.testswagger.Controller"))
//.paths(PathSelectors.ant())过滤器
.build()
;
}
//配置apiInfo的内容
private ApiInfo apiInfo() {
Contact contact= new Contact("hello", "https://www.baidu.com/", "3336107384@qq.com");
ApiInfo apiInfos=new ApiInfo(
"hello开发的第一个Swagger",
"加油!欧里给!!!!",
"1.0",
"https://www.bilibili.com/video/BV1PE411i7CV?p=48",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
return apiInfos;
}
}
Controller:
package com.testswagger.Controller;
import com.testswagger.pojo.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String toHello(){
return "hello";
}
@ApiOperation("方法声明注释")
@GetMapping("/user")
public User toUser(){
return new User();
}
}
Pojo:
package com.testswagger.pojo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Repository;
@ApiModel("用户模型")
@Repository
public class User {
//如果是string 则不公开,
@ApiModelProperty("名字说明")
public String name;
public String password;
}
4.yaml文件配置(重点解决自动根据环境开关swagger)
application.properties:
spring.profiles.active=dev
application-dev.yml:
server:
port:8080
5.效果图