Swagger overrides Path-Annotations

2019-03-04 05:27发布

I just got swagger to produces a valid swagger.json. I configured swagger by using the Application-config method. However, as soon as I override the getClasses-Method to add the swagger resouces, my JAX-RS Path-annotated classes stop working. The method looks like this

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new HashSet<>();

    resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
    resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

    return resources;
}

and invoking super.getClasses() returns am empty set. I got too many resources in my project, which I would not like to add manually.

Is there any way swagger does not mess with my previous configuration?

Thank you!

1条回答
放荡不羁爱自由
2楼-- · 2019-03-04 06:02

You can use a javax.ws.rs.core.Feature. Just register the classes through the callback's FeatureContext. Annotating the feature with @Provider will have it registered through the scanning.

@Provider
public class SwaggerFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        context.register(ApiListingResource.class);
        context.register(SwaggerSerializers.class);
        return true;
    }
}

But note that if the application is already registering the resources and providers by class-path scanning, I imagine it should also pick up the Swagger classes, as they are annotated with @Path[1] and @Provider[2]. Those are the annotations the class-path scan looks for.

I haven't tried it myself (I stopped using class-path scanning[3]), but have you tried just not registering them at all? In theory the class-path scan should pick it up.

1. io.swagger.jaxrs.listing.ApiListingResource
2. io.swagger.jaxrs.listing.SwaggerSerializers
3. When to Use JAX-RS Class-path Scanning Mechanism

查看更多
登录 后发表回答