先说结论:不要使用 2.9.2版本的swagger2,使用更低版本的 2.8.0
过程:
1、引入依赖
<!-- swagger -->
<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、Swagger2配置
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "rssoft.swaggerEnable", havingValue = "true")
@Scope
public class SwaggerConfig {
final String rootPackagePath = "com.rssoft.ykzly.api.controllers";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("公开分组")
.apiInfo(apiInfo())
.select()
//控制器的包路径
.apis(RequestHandlerSelectors.basePackage(rootPackagePath + ".OpenGroup"))
.paths(PathSelectors.regex("/_open_/.*"))
.build();
}
@Bean
public Docket controllerInternalGroup() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("token").description("用户token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.groupName("内部用户-接口分组")
.apiInfo(apiInfo())
.select()
//控制器的包路径InternalGroup
.apis(RequestHandlerSelectors.basePackage(rootPackagePath + ".InternalGroup"))
.paths(PathSelectors.regex("/_internal_/.*"))
.build()
.globalOperationParameters(pars);
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("API文档")
//描述
.description("描述")
.build();
}
}
3、创建接口
@ApiOperation(value = "添加使用者")
@RequestMapping(value = "/consumer/create", method = RequestMethod.POST)
public ApiResult consumerCreate(@RequestBody Consumer consumer,
@ApiParam(name = "usableTimes", value = "使用次数", required = true) @RequestParam(value = "usableTimes", required = true, defaultValue = "") Integer usableTimes) {
try {
Device device = deviceService.getById(consumer.getDeviceNo());
if (device.getAgentId() == null ) {
return ApiResult.builder().status(false).message("请先绑定代理商!").build();
}
consumerService.addConsumer(consumer, usableTimes, device);
return ApiResult.builder().status(true).message("添加成功!").build();
} catch (Exception e) {
e.printStackTrace();
return ApiResult.builder().status(false).message("添加失败!").errors(e.getMessage()).build();
}
}
4、在shiroConfiguration 文件把拦截放开
filterChainDefinitionMap.put("/swagger-ui.html","anon");
filterChainDefinitionMap.put("/swagger/**","anon");
filterChainDefinitionMap.put("/swagger2/**","anon");
filterChainDefinitionMap.put("/webjars/**", "anon");
filterChainDefinitionMap.put("/swagger-resources/**","anon");
filterChainDefinitionMap.put("/v2/**","anon");
5、启动项目后控制台报类型转换异常
6、在某度后查询解决方法后发现是swagger2自身错误,添加依赖可以解决
<!-- swagger -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</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>
7、访问页面后控制台无报错
8、准备运行该接口时页面报这个错误,而且看不到实体类的models
Errors
Hide
Resolver error at paths./_internal_/device/consumer/create.post.parameters.0.schema.$ref
Could not resolve reference because of:
9、多方查询无果后受到某个帖子老哥的启发 准备降低swagger2的 版本
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
10、运行后控制台无异常而且实体类的models可正常查看,问题解决。
前前后后被这些个问题折腾了接近俩个小时,记录一下遇到的这个坑,希望可以帮到和我遇到同样问题的兄弟!!