为什么我的swagger.json是空的? - RestEasy,Wildfly上的Java

The*_*son 6 java swagger swagger-2.0

我正在努力工作来记录使用RestEasy创建的API.

我尽可能地遵循这里提供的文档.

我有:

  1. 向pom.xml添加了招摇.
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-jaxrs</artifactId>
        <version>1.5.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 使用resteasy进行自动扫描,这意味着它存在于我的web.xml中我有context-param"resteasy.scan",其值为"true".
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
  1. 我告诉swagger我的API在哪里,并通过在我的web.xml中添加一个servlet来设置basepath
<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>
Run Code Online (Sandbox Code Playgroud)

我确实有几个工作端点

localhost:8080/mygiftlist/rest 
Run Code Online (Sandbox Code Playgroud)

这些都是从"com.jetnuts.rest"包中设置的.

但问题是,当我访问时:

http://localhost:8080/mygiftlist/rest/swagger.json
Run Code Online (Sandbox Code Playgroud)

我得到的只是一个空API:

{"swagger":"2.0","info":{"version":"1.0.0","title":""},"host":"localhost:8080","basePath":"/mygiftlist/rest","schemes":["http"]}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么这不会在包中拿起端点?我测试了端点,它们都有效.

例如,我有(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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此端点正常工作,并按预期添加到数据库,但它没有显示在swagger json中.

为了完整起见,我的"应用程序"类看起来像这样:

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

    public JaxRsActivator(){

    }
}
Run Code Online (Sandbox Code Playgroud)

最后它有效,但我不知道为什么?

将servlet移出web.xml并将其添加到"应用程序"后,它可以正常工作.我仍然不知道为什么会这样:

    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;
}
Run Code Online (Sandbox Code Playgroud)

luc*_*lli 0

这是一个很老的问题,但我今天遇到了同样的问题。建议的解决方案有效,但可能

  1. 您应该阅读 [1] 处的完整 Swagger JAX-RS 设置文档

  2. 您不需要任何web.xml,无需它即可完成所有 JAX-RS 。web.xml这是相当“老式”的

  3. 您不需要getClasses()中的方法JaxRsActivator,默认情况下 JAX-RS 会扫描所有发现 Web 服务的类。该文件中的重点是定义一个BeanConfig.

  4. 您应该使用注释记录您的 GiftListResourceRESTService 方法,请参阅 [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