zgrega
zgrega

Reputation: 43

Swagger Core Jersey 2.X with ResourceConfig

I am trying to add swagger to my application.

I am following this guide https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5#using-a-custom-application-subclass

but I have Jersey configured as ResourceConfig like this:

    @ApplicationPath("/resources/api")
     public class ApiApplication extends ResourceConfig {

    public ApiApplication() {
        super(MultiPartFeature.class);
        packages("my.rest.resources");
        register(io.swagger.jaxrs.listing.ApiListingResource.class);
        register(io.swagger.jaxrs.listing.SwaggerSerializers.class);

        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.2");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("/resources/api");
        beanConfig.setResourcePackage("my.rest.resources");
        beanConfig.setScan(true);
    }
  }

Is this correct?

There is no swagger.json generated.

According to my understanding swagger files should be on the following link http://localhost/api/api-docs/swagger.json

Updated:

Changed BasePath to resources/api

I also get the following exception in tomcat log:

    SEVERE: Servlet /XXXXXX threw load() exception
java.lang.NoSuchMethodError: org.reflections.util.ClasspathHelper.forPackage(Ljava/lang/String;[Ljava/lang/ClassLoader;)Ljava/util/Collection;
    at io.swagger.jaxrs.config.BeanConfig.classes(BeanConfig.java:189)
    at io.swagger.jaxrs.config.BeanConfig.setScan(BeanConfig.java:168)
    at my.rest.resources.apps.ApiApplication.<init>(ApiApplication.java:34)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

Upvotes: 4

Views: 4810

Answers (2)

Avi Nash
Avi Nash

Reputation: 1

java.lang.NoSuchMethodError: org.reflections.util.ClasspathHelper.forPackage(Ljava/lang/String;[Ljava/lang/ClassLoader;)Ljava/util/Collection; at io.swagger.jaxrs.config.BeanConfig.classes(BeanConfig.java:189) at io.swagger.jaxrs.config.BeanConfig.setScan(BeanConfig.java:168) at my.rest.resources.apps.ApiApplication.(ApiApplication.java:34) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

By the above stacktrace, its been observed as below:

Actually you are using Jersey1.x supported jar i.e reflections-0.9.9-RC1.jar, so add the Jersey 2.x supported jar reflections-0.9.9.jar to your libs folder or classpath. That will resolve the above issue

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209102

  1. The ApiListingResource (where the swagger data comes from), is nothing more than another JAX-RS (Jersey) resource class.

    @Path("/")
    public class ApiListingResource {
        ...
        @GET
        ...
        @Path("/swagger.{type:json|yaml}")
        public Response getListing(..) {...}
        ...
    }
    

    So the resource path would be the base @ApplicationPath("/resources/api") and the swagger resource path /swagger.json. So you would need to access /resources/api/swagger.json.

  2. You need to fix your base path in the BeanConfig. This base path is used to create the URLs for swagger UI. Using the base path "/api" means the URL data will cause the UI to access /api/yourresource which doesn't exist. The base path should be a combination of the servlet context (which I'm guessing in your case is none) and the Jersey base path (which is /resources/api). So set BeanConfig base path to "/resources/api"

Upvotes: 2

Related Questions