Why is my swagger.json empty? - RestEasy, Java on Wildfly
Asked Answered
D

1

7

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:

  1. Added swagger to the pom.xml.
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jaxrs</artifactId>
        <version>1.5.0</version>
    </dependency>
  1. 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>
  1. 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;
}
Dispense answered 15/12, 2015 at 17:23 Comment(0)
O
0

Quite an old question, however I had the same problem today. The suggested solution works, however probably

  1. You should read whole Swagger JAX-RS setup documentation at [1]

  2. You don't need any web.xml, you can do all JAX-RS without it. web.xml it's quite "old-fashioned"

  3. You don't need the method getClasses() in your JaxRsActivator, by default JAX-RS scans all your classes discovering webservices. The important point in this file is defining a BeanConfig.

  4. You should document your GiftListResourceRESTService methods with annotations, see [2]

[1] https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-RESTEasy-2.X-Project-Setup-1.5

[2] https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#quick-annotation-overview

Operatic answered 3/9, 2021 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.