简体   繁体   English

昂首阔步不生成REST文档

[英]Swagger not generating the REST documentation

I'm trying to let Swagger autogenerate che documentation of my REST APIs but I only get a partial result. 我试图让Swagger自动生成我的REST API的文档,但是我只得到了部分结果。

I'm using Resteasy. 我正在使用Resteasy。 I added the Maven Swagger dependency 我添加了Maven Swagger依赖项

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

Then I configured my 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 run the web application (on a Wildfly 9 server) and go to the URL http://localhost:8080/myapp/rest/swagger.json . 然后,我运行Web应用程序(在Wildfly 9服务器上)并转到URL http://localhost:8080/myapp/rest/swagger.json That's 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 build the REST documentation, even though my REST endpoints are reachable and are added to the Swagger list of resources. 似乎Swagger无法建立REST文档,即使我的REST端点可以访问并已添加到Swagger资源列表中。

What can be the problem? 可能是什么问题?

Thank you 谢谢

Giulio 朱利奥

Update: I checked that in the Swagger init method BeanConfig.classes() my REST classes are correctly discovered. 更新:我检查了在Swagger初始化方法BeanConfig.classes()中是否正确发现了我的REST类。

You need to add an @Api annotation to your resource classes. 您需要在资源类中添加@Api批注。

For example: 例如:

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 ();
    }
}

I think I got your problem. 我想我有你的问题。 Your root service extends Application that allows dynamic building of your resources hierarchy. 您的根服务扩展了允许动态构建资源层次结构的Application I believe that swagger even cannot support this technique because it generates its metadata (json files) at compile time. 我相信swagger甚至不能支持该技术,因为它在编译时会生成其元数据(json文件)。

I always use annotation based REST services, ie each resource is annotated with appropriate @Path annotation. 我总是使用基于注释的REST服务,即每个资源都带有适当的@Path注释。 The framework initializes all resources automatically, so I do not have to extend my root resource from Application and implement getClasses() . 该框架会自动初始化所​​有资源,因此我不必从Application扩展我的根资源并实现getClasses() In this case swagger can extract all your resources and generate json files at compile time by analyzing of JAXRS annotations like @Path , @PathParam , @GET , @POST etc. 在这种情况下招摇可以提取所有的资源,并通过JAXRS注解一样的分析生成JSON在编译时文件@Path@PathParam@GET@POST等。

You have to add @Api annotation to your resource class and load the resource package in setResourcePackage method. 您必须在资源类中添加@Api批注,并在setResourcePackage方法中加载资源包。 It should do the magic. 它应该做魔术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM