java 为什么 swagger ui 没有显示我的带注释的 REST 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32056515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:34:03  来源:igfitidea点击:

Why is the swagger ui not showing my annotated REST methods?

javarestjerseyswagger

提问by David Wood

I'm having trouble configuring swagger to see my REST methods. I'm working in Eclipse and Tomcat 7. I have the following simple REST method/class:

我在配置 swagger 以查看我的 REST 方法时遇到问题。我在 Eclipse 和 Tomcat 7 中工作。我有以下简单的 REST 方法/类:

package com.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api( value="/", description="Say hello class")
@Path("/")
public class Hello {

    @GET
    @Path("/hello")
    @ApiOperation(value="/hello", notes="hello method")
    public String sayHello() {
        return "Hello World!";
    }
}

and I'm using the following web.xml

我正在使用以下 web.xml

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>SwaggerTest</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.wordnik.swagger.jaxrs.json,com.rest</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                com.wordnik.swagger.jersey.listing.ApiListingResourceJSON,
                com.wordnik.swagger.jersey.listing.JerseyApiDeclarationProvider,
                com.wordnik.swagger.jersey.listing.JerseyResourceListingProvider
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>SwaggerJerseyJaxrsConfig</servlet-name>
        <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>0.0.1</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/api/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

</web-app>

The rest service is available on http://localhost:8080/SwaggerTest/api/sayHelloand presents the proper message in the browser. The swagger spec for the service is available at http://localhost:8080/SwaggerTest/api-docs. However, all that is returned is

其余服务在http://localhost:8080/SwaggerTest/api/sayHello上可用,并在浏览器中显示正确的消息。该服务的 swagger 规范可从http://localhost:8080/SwaggerTest/api-docs 获得。然而,返回的只是

{"apiVersion":"0.0.1","swaggerVersion":"1.2","apis":[{"path":"/","description":"Say hello class"}]}

What happened to the GET sayHello() method? Or is that all it is supposed to return?

GET sayHello() 方法发生了什么变化?或者这就是它应该返回的全部?

Any help will be greatly appreciate. Thanks in advance.

任何帮助将不胜感激。提前致谢。

David

大卫

P.S. the maven dependencies are

PS Maven 依赖项是

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-Hymanson</artifactId>
        <version>2.7</version>
    </dependency>

    <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
        <version>1.3.12</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>2.1.1</version>
    </dependency>

    <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-servlet_2.10</artifactId>
        <version>1.3.12</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.7</version>
    </dependency>


</dependencies>

回答by Ron

The problem is that you're using swagger-core 1.3 which produces Swagger 1.2 definitions. Swagger 1.2 wasn't too fond of root (/) based APIs. You can still show it if you change the valueof @Apito anything other than "/". This does not affect the API itself, just how the documentation is hosted.

问题是您使用的是 swagger-core 1.3,它生成 Swagger 1.2 定义。Swagger 1.2 不太喜欢基于根 (/) 的 API。如果将valueof更改为@Api“/”以外的任何内容,您仍然可以显示它。这不会影响 API 本身,只会影响文档的托管方式。

If you give it the valueof "/root" for example, and then go to http://localhost:8080/SwaggerTest/api-docs/root- you'll see your exposed service.

value例如,如果你给它“/root”,然后转到http://localhost:8080/SwaggerTest/api-docs/root- 你会看到你暴露的服务。

Also, you're using an old version of both swagger-core and Swagger in general. It looks like you're trying to integrate with Jersey, so you can follow https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-1.X-Project-Setup-1.5as your integration guide. This produces Swagger 2.0 which doesn't have the same issue with root resources.

此外,您通常使用 swagger-core 和 Swagger 的旧版本。看起来您正在尝试与 Jersey 集成,因此您可以按照https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-1.X-Project-Setup-1.5作为您的集成指南。这会产生 Swagger 2.0,它与 root 资源没有相同的问题。

回答by David Wood

I had not told jersey to examine the correct packages in swagger. In particular com.wordnik.swagger.jaxrs.listing, had to be added as follows:

我没有告诉 jersey 大摇大摆地检查正确的包裹。特别是 com.wordnik.swagger.jaxrs.listing,必须添加如下:

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.ibm.dipo.rest,
                com.wordnik.swagger.jaxrs.listing</param-value>
</init-param>

I then

然后我

  1. loaded the swagger dist tree (https://github.com/swagger-api/swagger-ui/tree/master/dist) into my WebContent directory,
  2. renamed it to api-docs,
  3. Modified index.html to point to swagger's api-docs location:

    $(function () { var url = window.location.search.match(/url=([^&]+)/);

      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "http://petstore.swagger.io/v2/swagger.json";
        url = "../../api/BlueHound/api-docs"
      }
    
  1. 将 swagger dist 树(https://github.com/swagger-api/swagger-ui/tree/master/dist)加载到我的 WebContent 目录中,
  2. 将其重命名为 api-docs,
  3. 修改 index.html 以指向 swagger 的 api-docs 位置:

    $(function () { var url = window.location.search.match(/url=([^&]+)/);

      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "http://petstore.swagger.io/v2/swagger.json";
        url = "../../api/BlueHound/api-docs"
      }
    

With these changes, my swagger doc is available at http://localhost:8080/RTSServices/api-docs/index.html

通过这些更改,我的 swagger 文档可在http://localhost:8080/RTSServices/api-docs/index.html 获得

The complete web.xml follows:

完整的 web.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
    <display-name>RTSServices</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.ibm.dipo.rest,
                        com.wordnik.swagger.jaxrs.listing</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/api/BlueHound/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>SwaggerJerseyJaxrsConfig</servlet-name>
        <servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>0.0.1</param-value>
        </init-param>
        <init-param>
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/RTSServices/api/BlueHound/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

</web-app>