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

15,449

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 value of @Api to anything other than "/". This does not affect the API itself, just how the documentation is hosted.

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

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.5 as your integration guide. This produces Swagger 2.0 which doesn't have the same issue with root resources.

Share:
15,449
David Wood
Author by

David Wood

Updated on June 08, 2022

Comments

  • David Wood
    David Wood about 2 years

    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:

    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

       <?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/sayHello and 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

    {"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?

    Any help will be greatly appreciate. Thanks in advance.

    David

    P.S. the maven dependencies are

    <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>
    
  • David Wood
    David Wood almost 9 years
    And to Ron's answer, I didn't have to change any of my maven dependencies.
  • Michael Lucas
    Michael Lucas over 8 years
    I think you should accept this answer (your own) as the answer to your question ...