Swagger does not create REST documentation

I'm trying to let Swagger auto-generate the documentation for my REST APIs, but I get only a partial result.

I am using Resteasy. I added Maven Swagger dependency

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jaxrs</artifactId>
    <version>1.5.3</version>
</dependency>

Then I set up the Application object

package com.myapp.init;


import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;
import io.swagger.jaxrs.listing.SwaggerSerializers;


@ApplicationPath("/rest")
public class WebappInit extends Application {

    public WebappInit() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("theIP:8080");
        beanConfig.setBasePath("/myapp/rest/");
        beanConfig.setResourcePackage("the.resource.package");
        beanConfig.setScan(true);
        beanConfig.setPrettyPrint(true);

    }


    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();


        // here I add my REST WSs
        s.add(ApiListingResource.class);
        s.add(SwaggerSerializers.class);


        return s;
    }

}

Then I launch the web application (on the Wildfly 9 server) and go to the URL http://localhost:8080/myapp/rest/swagger.json. What i get

{
  swagger: "2.0",
  info: {
    version: "1.0.0"
  },
  host: "10.17.36.215:8080",
  basePath: "/devops/rest/",
  schemes: [
    "http"
  ]
}

It seems that Swagger cannot create REST documentation, although my REST endpoints are reachable and added to the Swagger resource list.

What could be the problem?

thanks

Giulio

Update: I checked that in the Swagger initialization method, BeanConfig.classes()my REST classes were correctly detected.

+5
source share
3

@Api .

:

package my.sample;
import io.swagger.annotations.Api;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;

@Api
@Path ("/mypath")
public class MyResource
{
    @GET
    public Response myEndpoint()
    {
        return Response.ok ();
    }
}
+5

, . Application, . , , (json ) .

REST , .. @Path. , Application getClasses(). swagger json , JAXRS, @Path, @PathParam, @GET, @POST ..

0

You must add the annotation @Apito your resource class and load the resource package in the setResourcePackage method. That should do the magic.

0
source

Source: https://habr.com/ru/post/1618087/


All Articles