2
votes

I am using Swagger for Jersey2 according to this tutorial: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 with custom Application class. In order for Swagger to initialize properly, an override of getClasses() needs to be implemented.

Unfortunately this also means that I must manually add every single class from my resources to the HashSet. If I don't do that all endpoints return 404. But when I am not using Swagger I don't have to override the method and all my REST resources are automatically discovered by JAX-RS annotations.

Is there a way to register Swagger classes but at the same time retain automatic discovery for my personal resources?

1

1 Answers

2
votes

Use a ResourceConfig, which extends Application. ResourceConfig has a packages method to register all classes by package scanning, and you can register the Swagger components calling register on the ResourceConfig. You can do all this in the constructor.

@ApplicationPath("/api")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("the.packages.to.scan");
        register(SwaggerComponent.class);
    }
}

See Also:

  • Swagger overrides Path-Annotations for another option. It uses standard JAX-RS APIs. But see also the link about classpath scanning and why you shouldn't use it. Package scanning it better, and faster.