使用Application子类时,Swagger没有显示泽西的@Api注释

时间:2015-09-07 14:22:08

标签: java rest jersey swagger

我正在尝试使用swagger记录我的球衣REST API,但我无法让它记录任何方法或API。当我访问host / api / swagger.json并且关于它时,我可以获得我在MainAPI中设置的属性。 我有一个名为MainAPI的主要Application子类,如下所示:

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;
}
}

然后我有另一个类定义了一些名为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上使用jersey 1.13运行它。

1 个答案:

答案 0 :(得分:2)

在Swagger配置中的某个地方(json,xml或通过代码)应该有一个资源包。您需要添加带注释的类&#39;包装到那,所以swagger将自动拾取它们。 像

这样的东西
 beanConfig.setResourcePackage("my.package.is.the.best");