简体   繁体   中英

Swagger is not showing @Api annotation for jersey when using Application subclass

I am trying to document my jersey REST API using swagger, but I can not manage to get it to document any method or API. I can get the properties that I set in the MainAPI when I access host/api/swagger.json and that's about it. I have 1 main Application subclass called MainAPI which looks like this:

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

Then I have another class which defines some api functionality called FirstAPI. Code looks like this:

@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();
        }
    }
   } 

I am running this on Tomcat 7 with jersey 1.13.

Somewhere in your Swagger configuration (either json, xml or via code) there should be a resources package. You need to add your annotated classes' packages to that, so that swagger will auto-pick them up. Something like

 beanConfig.setResourcePackage("my.package.is.the.best"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM