版本
SpringBoot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
</parent>
Shiro版本
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
Swagger版本
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.1</version>
</dependency>
整合
SwaggerConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
// 生产(prod)环境不启用swagger
@Profile({"dev", "test"})
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter {
// 扫描那个包下的controller
@Bean
public Docket allDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("all")
.select()
.apis(RequestHandlerSelectors.basePackage("com.wedowork.modules"))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
// 可以按照不同的包进行分组
// @Bean
// public Docket apiDocket() {
// return new Docket(DocumentationType.SWAGGER_2)
// .apiInfo(apiInfo())
// .groupName("api")
// .select()
// .apis(RequestHandlerSelectors.basePackage("com.wedowork.modules.api"))
// .paths(PathSelectors.any())
// .build()
// .securitySchemes(securitySchemes())
// .securityContexts(securityContexts());
// }
// 用于设置shiro校验的token
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeys = new ArrayList<>();
apiKeys.add(new ApiKey("Authorization", "token", "header"));
return apiKeys;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("^(?!auth).*$")).build());
return securityContexts;
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> securityReferences = new ArrayList<>();
securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
return securityReferences;
}
// swagger描述
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("系统API文档")
.description("项目重构")
.contact(new Contact("name","url","email"))
.version("1.0.0")
.build();
}
// 设置访问swagger-bootstrap-ui的路径
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Shiro
filterMap.put("/swagger/**", "anon");
filterMap.put("/swagger2/**", "anon");
filterMap.put("/swagger*", "anon");
filterMap.put("/swagger*/**", "anon");
filterMap.put("/webjars/**", "anon");
filterMap.put("/doc.html", "anon");
filterMap.put("/swagger-ui.html", "anon");
filterMap.put("/swagger-resources/**", "anon");
放开swagger用到一些路径和静态资源
正常情况下启动项目访问即可
http://ip:port/doc.html
遇到问题
因为我这个项目年代比较旧,SpringBoot+thymeleaf的单体项目,现在要重构成前后端分离的,前端需要一个接口文档,想简单点就直接整合swagger当作接口文档。 启动项目时发生了栈溢出
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.StackOverflowError
...
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.StackOverflowError
...
Caused by: java.lang.StackOverflowError: null
看到一篇博客:https://www.jianshu.com/p/470e4167ad46 考虑到可能是入参对象出现循环嵌套的问题,开始一个包一个包的排查,最终锁定到这段代码
// controller
@RequestMapping("/save")
public W save(@RequestBody WaterPumpRoom waterPumpRoom) {
}
// 实体类一
@Setter
@Getter
@ToString
public class WaterPumpRoom{
private List<WaterPump> waterPumpList;
}
// 实体类二
@Setter
@Getter
@ToString
public class WaterPump {
private List<WaterPumpRoom> waterPumpRoomList;
}
此接口并没有被使用,直接注释这个接口后启动成功
Map接口
系统中大量接口都是使用map入参和返回的,所以这里我简单介绍一些swagger对map类型的支持 get请求map入参、接口返回map中携带对象
@ApiOperation("列表")
@GetMapping("/list")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "page", value = "当前页数", dataType = "int", required = true),
@ApiImplicitParam(paramType = "query", name = "limit", value = "每页显示条目个数", dataType = "int", required = true),
@ApiImplicitParam(paramType = "query", name = "organizationId", value = "条件ID", dataType = "String")
})
@ApiResponses({
@ApiResponse(code = 0, message = "ok", response = SlightShockSiteinFoEntity.class),
})
public Map list1(@ApiIgnore @RequestParam Map<String, Object> params) {
SlightShockSiteinFoEntity entity = new SlightShockSiteinFoEntity();
Map<String,Object> map = new HashMap<>();
map.put("data", entity);
return map;
}
|