Java 未调用Swagger ReaderListener
我试图从ReaderListener接口调用afterScan(…)方法,但没有被调用。这是我的密码:Java 未调用Swagger ReaderListener,java,web-services,jax-rs,swagger,swagger-2.0,Java,Web Services,Jax Rs,Swagger,Swagger 2.0,我试图从ReaderListener接口调用afterScan(…)方法,但没有被调用。这是我的密码: @SwaggerDefinition( info = @Info( description = "My API", version = "V1.2.3", title = "The only API you'll ever need", termsOfService = "share and care", contact = @Contact(name
@SwaggerDefinition( info = @Info(
description = "My API",
version = "V1.2.3",
title = "The only API you'll ever need",
termsOfService = "share and care",
contact = @Contact(name = "Test-Bob", email = "test-bob@tester.io", url = "http://swagger.io"),
license = @License(name = "Apache 2.0", url = "http://www.apache.org")))
@Api(tags = {"something", "else"})
@Path("/myUrl/swagger/{type:json|yaml}")
public class MyApiListingResource extends BaseApiListingResource implements ReaderListener {
private static final Logger logger = LoggerFactory.getLogger(MyApiListingResource.class);
@Context ServletContext context;
@GET
@Produces({MediaType.APPLICATION_JSON, "application/yaml"})
@ApiOperation(value = "The swagger definition in either JSON or YAML",
hidden = true)
public Response getListing(@Context Application app, @Context ServletConfig servletConfig, @Context HttpHeaders headers,
@Context UriInfo uriInfo, @PathParam("type") String type) throws JsonProcessingException {
if (isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
return getListingYamlResponse(app, context, servletConfig, headers, uriInfo);
} else {
System.out.println("I found json!");
logger.debug("I'm also before!!!");
return getListingJsonResponse(app, context, servletConfig, headers, uriInfo);
}
}
@Override
public void beforeScan(Reader reader, Swagger swagger) {
System.out.println("I'm before!!!");
logger.debug("I'm also before!!!");
}
@Override
public void afterScan(Reader reader, Swagger swagger) {
System.out.println("I'm after!!!");
logger.debug("I'm also after!!!");
}
}
现在,当我调用以下服务时:
http://localhost:9081/myApp/myUrl/swagger/json
我确实在控制台和日志中收到了“我找到了json!”,但控制台或日志中没有打印任何“before”或“after”注释
我使用的是Swagger 1.5.10和Java7。我会提供任何其他需要的细节
欢迎任何意见!谢谢
更新:我尝试将@SwaggerDefinition注释添加到这个类中,如上图所示,但仍然不起作用
你知道我遗漏了什么吗?我终于找到了问题所在。显然,原始设计的问题在于,我在试图实现ReaderListener的同一个类上扩展了
BaseApiListingResource
。无论出于何种原因(如果您知道原因,请随意评论),ReaderListener
在我扩展BaseApiListingResource
时不起作用。如果我在一个不扩展BaseApiListInSource
的类上实现ReaderListener
,那么在扫描前和扫描后都可以正常工作
希望这对某人有帮助