使用 Application 子类时,Swagger 未显示球衣的 @Api 注释
Swagger is not showing @Api annotation for jersey when using Application subclass
我正在尝试使用 swagger 记录我的球衣 REST API,但我无法设法让它记录任何方法或 API。当我访问 host/api/swagger.json 时,我可以获得在 MainAPI 中设置的属性,仅此而已。
我有 1 个名为 MainAPI 的主应用程序 subclass,如下所示:
ApplicationPath("api")
public class MainAPI extends Application{
public MainAPI(){
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setBasePath("api");
beanConfig.setResourcePackage("io.swagger.resources");
beanConfig.setScan(true);
}
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet();
resources.add(FirstAPI.class);
resources.add(SecondAPI.class);
resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}
}
然后我有另一个 class,它定义了一些叫做 FirstAPI 的 api 功能。代码如下所示:
@Path("login")
@Api(value="user", description = "Login and check login status")
public class FirstAPI extends MainAPI {
@GET
@ApiOperation(value = "Test method",
notes = "This is test method")
@Path("test")
public Response testMethod() {
return Response.status(200).entity("Hello Martians!").build();
}
@GET
@ApiOperation(value = "Check if any user is loggedin",
notes = "If an user is loggedin the username will be returned",
response = Response.class)
// @Path("/login")
public Response checkLogin(@Context HttpServletRequest request) {
JSONObject myJson = new JSONObject();
if (true) {
myJson.put("loggedin", "true");
myJson.put("user", "John Doe");
return Response.status(200).entity(myJson.toString()).build();
} else {
myJson.put("loggedin", "false");
return Response.status(200).entity(myJson.toString()).build();
}
}
}
我 运行 这身 Tomcat 7 球衣 1.13。
在您的 Swagger 配置中的某处(json、xml 或通过代码)应该有一个资源包。您需要将带注释的 类' 包添加到其中,这样 swagger 就会自动拾取它们。
像
beanConfig.setResourcePackage("my.package.is.the.best");
我正在尝试使用 swagger 记录我的球衣 REST API,但我无法设法让它记录任何方法或 API。当我访问 host/api/swagger.json 时,我可以获得在 MainAPI 中设置的属性,仅此而已。 我有 1 个名为 MainAPI 的主应用程序 subclass,如下所示:
ApplicationPath("api")
public class MainAPI extends Application{
public MainAPI(){
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.2");
beanConfig.setBasePath("api");
beanConfig.setResourcePackage("io.swagger.resources");
beanConfig.setScan(true);
}
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet();
resources.add(FirstAPI.class);
resources.add(SecondAPI.class);
resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}
}
然后我有另一个 class,它定义了一些叫做 FirstAPI 的 api 功能。代码如下所示:
@Path("login")
@Api(value="user", description = "Login and check login status")
public class FirstAPI extends MainAPI {
@GET
@ApiOperation(value = "Test method",
notes = "This is test method")
@Path("test")
public Response testMethod() {
return Response.status(200).entity("Hello Martians!").build();
}
@GET
@ApiOperation(value = "Check if any user is loggedin",
notes = "If an user is loggedin the username will be returned",
response = Response.class)
// @Path("/login")
public Response checkLogin(@Context HttpServletRequest request) {
JSONObject myJson = new JSONObject();
if (true) {
myJson.put("loggedin", "true");
myJson.put("user", "John Doe");
return Response.status(200).entity(myJson.toString()).build();
} else {
myJson.put("loggedin", "false");
return Response.status(200).entity(myJson.toString()).build();
}
}
}
我 运行 这身 Tomcat 7 球衣 1.13。
在您的 Swagger 配置中的某处(json、xml 或通过代码)应该有一个资源包。您需要将带注释的 类' 包添加到其中,这样 swagger 就会自动拾取它们。 像
beanConfig.setResourcePackage("my.package.is.the.best");