I am trying to get swagger working to document my API created using RestEasy.
I've followed the documentation available here, as closely as I can.
I have:
- Added swagger to the pom.xml.
<dependency> <groupId>io.swagger</groupId> <artifactId>swagger-jaxrs</artifactId> <version>1.5.0</version> </dependency>
- Used automatic scanning with resteasy, which means this exists in my web.xml I have the context-param "resteasy.scan" with value "true".
<context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param>
- I have told swagger where my API is and set up the basepath 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 do have several working endpoints under
localhost:8080/mygiftlist/rest
which are all set up from the "com.jetnuts.rest" package.
However, the problem is, when I visit:
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 wouldn't be picking up the endpoints in in the package? I have tested the endpoints and they all work.
For example, I have in (com.jetnuts.rest):
/**
* Created by steve on 13/12/2015.
*/
@Path("/giftlists")
@RequestScoped
public class GiftListResourceRESTService {
@Inject
CrudService dao;
/**
* Create a new list
* @param giftList The gift list to create
* @return A created response, or an error if the incorrect format is given.
*/
@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 is adding to the database as expected, but it doesn't show in the swagger json.
For completeness, my "Application" class looks like this:
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
public JaxRsActivator(){
}
}
Finally it works but I don't know why?
After moving the servlet out of the web.xml and adding this 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;
}