为什么招摇ui没有显示我的注释REST方法?

时间:2015-08-17 17:40:21

标签: java rest jersey swagger

我无法配置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!";
    }
}

我正在使用以下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>

http://localhost:8080/SwaggerTest/api/sayHello上提供了其他服务,并在浏览器中显示正确的消息。该服务的招摇规范可在http://localhost:8080/SwaggerTest/api-docs获得。但是,所有返回的都是

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

GET sayHello()方法怎么了?或者是它应该归还的全部?

任何帮助都将非常感激。提前谢谢。

大卫

P.S。 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-jackson</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>

2 个答案:

答案 0 :(得分:1)

问题在于您使用swagger-core 1.3来生成Swagger 1.2定义。 Swagger 1.2并不太喜欢基于根(/)的API。如果您将value的{​​{1}}更改为&#34; /&#34;以外的任何内容,您仍然可以展示它。这不会影响API本身,也不会影响文档的托管方式。

如果你给它{&#34; / root&#34; @Api例如,然后转到http://localhost:8080/SwaggerTest/api-docs/root - 您将看到您的公开服务。

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

答案 1 :(得分:0)

我没有告诉球衣在招摇时检查正确的包裹。特别是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>

然后我

  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 =([^&amp;] +)/);

      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "http://petstore.swagger.io/v2/swagger.json";
        url = "../../api/BlueHound/api-docs"
      }
    
  4. 通过这些更改,我可以在http://localhost:8080/RTSServices/api-docs/index.html

    找到我的招摇文档

    完整的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>