集成swagger2的时候swagger-ui.html页面的v2/api-docs接口报404
尝试网上说的权限、包版本不一致、资源路径映射问题,发现都没有问题。
单独访问v2/api-docs接口的时候报
Swagger2Controller Unable to find specification for group
查看相关代码:
@ResponseBody
public ResponseEntity<Json> getDocumentation(
@RequestParam(value = "group", required = false) String swaggerGroup,
HttpServletRequest servletRequest) {
String groupName = Optional.fromNullable(swaggerGroup).or(Docket.DEFAULT_GROUP_NAME);
Documentation documentation = documentationCache.documentationByGroup(groupName);
if (documentation == null) {
LOGGER.warn("Unable to find specification for group {}", groupName);
return new ResponseEntity<Json>(HttpStatus.NOT_FOUND);
}
Swagger swagger = mapper.mapDocumentation(documentation);
UriComponents uriComponents = componentsFrom(servletRequest, swagger.getBasePath());
swagger.basePath(Strings.isNullOrEmpty(uriComponents.getPath()) ? "/" : uriComponents.getPath());
if (isNullOrEmpty(swagger.getHost())) {
swagger.host(hostName(uriComponents));
}
return new ResponseEntity<Json>(jsonSerializer.toJson(swagger), HttpStatus.OK);
}
groupName 是一个空字符串,难道是这个groupName也需要设置 就是需要设置一个分组名称
在配置Docket的地方设置groupName属性
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).groupName("default_1").apiInfo(apiInfo())
.select()
// RequestHandlerSelectors配置扫描接口的方式
.apis(RequestHandlerSelectors.any())
// path过滤什么路径
.paths(PathSelectors.any())
.build();
}
重启服务问题解决。