I am trying to get swagger to work on documenting my API created using RestEasy.
I followed the available documentation here as much as possible.
I have:
- Added swagger to pom.xml.
<dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jaxrs</artifactId> <version>1.5.0</version> </dependency>
- Auto scan using resteasy is used, which means it exists in my web.xml. I have a context parameter "resteasy.scan" with a value of "true".
<context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param>
- I told where my API is, and set up the base path by adding a servlet to my web.xml
<servlet> <servlet-name>Jersey2Config</servlet-name> <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class> <init-param> <param-name>api.version</param-name> <param-value>1.0.0</param-value> </init-param> <init-param> <param-name>swagger.api.basepath</param-name> <param-value>http://localhost:8080/mygiftlist/rest</param-value> </init-param> <init-param> <param-name>swagger.api.resourcePackage</param-name> <param-value>com.jetnuts.rest</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
I have several working endpoints in
localhost:8080/mygiftlist/rest
which are configured from the com.jetnuts.rest package.
However, the problem is when I am:
http://localhost:8080/mygiftlist/rest/swagger.json
All I get is an empty API:
{"swagger":"2.0","info":{"version":"1.0.0","title":""},"host":"localhost:8080","basePath":"/mygiftlist/rest","schemes":["http"]}
Does anyone know why this will not collect endpoints in a package? I tested endpoints and they all work.
For example, I have (com.jetnuts.rest):
@Path("/giftlists") @RequestScoped public class GiftListResourceRESTService { @Inject CrudService dao; @POST @Path("/") @Consumes(MediaType.APPLICATION_JSON) public Response createGift(GiftList giftList) { dao.create(giftList); try { URI resourceURI = new URI("/giftlists/" + giftList.getId()); return Response.created(resourceURI).build(); }catch(URISyntaxException e){ return Response.serverError().build(); } } }
This endpoint works and adds to the database as expected, but it does not appear in swagger json.
For completeness, my Application class is as follows:
@ApplicationPath("/rest") public class JaxRsActivator extends Application { public JaxRsActivator(){ } }
Finally it works, but I donβt know why?
After moving the servlet from web.xml and adding it to the "application", it works. I still don't know why this is:
public JaxRsActivator(){ BeanConfig beanConfig = new BeanConfig(); beanConfig.setVersion("1.0.2"); beanConfig.setSchemes(new String[]{"http"}); beanConfig.setHost("localhost:8080"); beanConfig.setBasePath("/rest/"); beanConfig.setResourcePackage("com.jetnuts.rest"); beanConfig.setScan(true); } @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; }