学习地址
https://www.bilibili.com/video/av64841843
https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html
创建项目引入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<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>
创建swagger配置类
package com.lv.swaggerdemo.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2//开启Swagger
public class SwaggerConfig {
}
访问swagger页面
访问:http://localhost:8080/swagger-ui.html
swagger页面
配置swagger信息
package com.lv.swaggerdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
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 docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("lv", "baidu.com", "test@qq.com");
return new ApiInfo(
"测试title",
"测试描述",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
Swagger配置扫描接口
package com.lv.swaggerdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.function.Predicate;
@Configuration
@EnableSwagger2//开启Swagger
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
//配置swagger信息
.apiInfo(apiInfo())
//配置是否启用swagger,false为关闭
.enable(false)
.select()
//RequestHandlerSelectors配置要扫描接口的方式
//RequestHandlerSelectors.basePackage指定要扫描的包
//any()扫描全部
//none()全部不扫描
//withMethodAnnotation:扫描方法上的注解
//withClassAnnotation:扫描类上的注解
.apis(RequestHandlerSelectors.basePackage("com.lv.swaggerdemo.controller"))
//paths()。过滤什么路径(如下:过滤所有/hello开头的请求)
// .paths(PathSelectors.ant("/hello/**"))
.build()
;
}
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("lv", "baidu.com", "test@qq.com");
return new ApiInfo(
"测试title",
"测试描述",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
设置生产环境才启用swagger
//设置要显示的swagger环境
Profiles dev = Profiles.of("dev");
//通过environment.acceptsProfiles()判断处在自己设置的环境中
boolean flag = environment.acceptsProfiles(dev);
return new Docket(DocumentationType.SWAGGER_2)
//配置swagger信息
.apiInfo(apiInfo())
//配置是否启用swagger,false为关闭
.enable(flag)
.select()
//RequestHandlerSelectors配置要扫描接口的方式
//RequestHandlerSelectors.basePackage指定要扫描的包
//any()扫描全部
//none()全部不扫描
//withMethodAnnotation:扫描方法上的注解
//withClassAnnotation:扫描类上的注解
.apis(RequestHandlerSelectors.basePackage("com.lv.swaggerdemo.controller"))
//paths()。过滤什么路径(如下:过滤所有/hello开头的请求)
// .paths(PathSelectors.ant("/hello/**"))
.build()
;
}
配置多个分组
//如果需要多个分组,则配置多个swagger
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("测试组2");
}
@Bean
public Docket docket(Environment environment) {
//设置要显示的swagger环境
Profiles dev = Profiles.of("dev");
//通过environment.acceptsProfiles()判断处在自己设置的环境中
boolean flag = environment.acceptsProfiles(dev);
return new Docket(DocumentationType.SWAGGER_2)
//配置swagger信息
.apiInfo(apiInfo())
//设置分组
.groupName("测试组")
接口注释
controller中
//只要接口中存在实体类,它就会被扫描到wagger中
@PostMapping("/user")
//ApiOperation接口,描述接口的
@ApiOperation("Hello控制类")
public User user(@ApiParam("用户名") String username){
return new User();
}
实体类中
package com.lv.swaggerdemo.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
springboot中shiro过滤swagger的请求
网上有些文章是这样配置的,但是我测试后不能正常访问
filterMap.put("/swagger-ui.html", "anon");
filterMap.put("/swagger-resources/**", "anon");
filterMap.put("/v2/**", "anon");
filterMap.put("/webjars/**", "anon");
改为以下配置后生效,在shiro配置文件中:
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
//不需要权限也能访问
chainDefinition.addPathDefinition("/swagger-ui.html", "anon");
chainDefinition.addPathDefinition("/swagger-resources", "anon");
chainDefinition.addPathDefinition("/swagger-resources/configuration/security", "anon");
chainDefinition.addPathDefinition("/swagger-resources/configuration/ui", "anon");
chainDefinition.addPathDefinition("/v2/api-docs", "anon");
chainDefinition.addPathDefinition("/webjars/springfox-swagger-ui/**", "anon");
// 其他所有页面必须验证
chainDefinition.addPathDefinition("/**", "authc");
return chainDefinition;
}