一、集成
①环境依赖
<!-- 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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
②简单测试
package com.gg.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
package com.gg.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //标记为配置类
@EnableSwagger2 //启动swagger
public class SwaggerConfig {
}
③访问页面
http://localhost:8080/swagger-ui.html#/
二、配置
①配置swagger信息
package com.gg.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("阿国","测试网址","1111@qq.com");
return new ApiInfo(
"学习用1",
"学习用2",
"学习用3",
"学习用4",
contact,
"学习用5",
"学习用6",
new ArrayList()
);
}
}
②配置接口信息
.apiInfo(apiInfo对象)
package com.gg.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// RequestHandlerSelectors:配置要扫描的接口方式
// basePackage:指定要扫描的包
// any():扫描全部
// none():不扫描
// withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
// withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.gg.controller"))
// paths():过滤什么路径
.paths(PathSelectors.ant("/gg/**"))
.build()
;
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("阿国","测试网址","1111@qq.com");
return new ApiInfo(
"学习用1",
"学习用2",
"学习用3",
"学习用4",
contact,
"学习用5",
"学习用6",
new ArrayList()
);
}
}
③配置是否启动
.enable(true || false)
package com.gg.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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实例
// enable():如果为false
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false)
.select()
// paths():过滤什么路径
//.paths(PathSelectors.ant("/gg/**"))
.build()
;
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("阿国","测试网址","1111@qq.com");
return new ApiInfo(
"学习用1",
"学习用2",
"学习用3",
"学习用4",
contact,
"学习用5",
"学习用6",
new ArrayList()
);
}
}
④配置分组
.groupName(分组名)
package com.gg.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 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实例
// enable():如果为false则关闭
@Bean
public Docket docket(Environment environment){
// 设置要显示的swagger的环境
Profiles profiles=Profiles.of("dev","test1");
// 通过environment.acceptsProfiles判断是否处于自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("阿国")
.enable(flag) // enable是否启动swagger
.select()
// paths():过滤什么路径
//.paths(PathSelectors.ant("/gg/**"))
.apis(RequestHandlerSelectors.basePackage("com.gg.controller"))
.build()
;
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("阿国","测试网址","1111@qq.com");
return new ApiInfo(
"学习用1",
"学习用2",
"学习用3",
"学习用4",
contact,
"学习用5",
"学习用6",
new ArrayList()
);
}
}
配置多个分组
package com.gg.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 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 {
// 配置多个分组
@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实例
// enable():如果为false则关闭
@Bean
public Docket docket(Environment environment){
// 设置要显示的swagger的环境
Profiles profiles=Profiles.of("dev","test1");
// 通过environment.acceptsProfiles判断是否处于自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("阿国")
.enable(flag) // enable是否启动swagger
.select()
// paths():过滤什么路径
//.paths(PathSelectors.ant("/gg/**"))
.apis(RequestHandlerSelectors.basePackage("com.gg.controller"))
.build()
;
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("阿国","测试网址","1111@qq.com");
return new ApiInfo(
"学习用1",
"学习用2",
"学习用3",
"学习用4",
contact,
"学习用5",
"学习用6",
new ArrayList()
);
}
}
三、在生产环境中使用,在发布时不使用
①properties文件配置
端口号分别为:8080、8081、8082
② 获取选定的环境
package com.gg.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 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实例
// enable():如果为false则关闭
@Bean
public Docket docket(Environment environment){
// 设置要显示的swagger的环境
Profiles profiles=Profiles.of("dev","test1");
// 通过environment.acceptsProfiles判断是否处于自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag) // enable是否启动swagger
.select()
// paths():过滤什么路径
//.paths(PathSelectors.ant("/gg/**"))
.apis(RequestHandlerSelectors.basePackage("com.gg.controller"))
.build()
;
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
Contact contact=new Contact("阿国","测试网址","1111@qq.com");
return new ApiInfo(
"学习用1",
"学习用2",
"学习用3",
"学习用4",
contact,
"学习用5",
"学习用6",
new ArrayList()
);
}
}
四、实体类和接口配置
pojo实体类对象
package com.gg.pojo;
public class User {
public String username;
public String password;
}
package com.gg.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类") // 相当于给实体类注释
public class User {
@ApiModelProperty("用户名") // 给变量表注释
public String username;
@ApiModelProperty("密码")
public String password;
}
接口配置
package com.gg.controller;
import com.gg.pojo.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@GetMapping("/hello")
public String hello(){
return "hello";
}
// 只要接口中,返回值中存在实体类,就会被扫描的swagger
@PostMapping("/user")
public User user(){
return new User();
}
}
接口类注释
package com.gg.controller;
import com.gg.pojo.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Hello {
@GetMapping("/hello")
public String hello(){
return "hello";
}
// 只要接口中,返回值中存在实体类,就会被扫描的swagger
@PostMapping("/user")
public User user(){
return new User();
}
@ApiOperation("hello控制类")
@GetMapping("/hello2")
public String hello2(String username){
return "hello"+username;
}
}
接口参数api注释