Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 980
David Wood Avatar asked Aug 17 '15 17:08

David Wood


People also ask

How do I use REST API with Swagger?

Use Swagger Inspector to quickly generate your OAS-based documentation for existing REST APIs by calling each end point and using the associated response to generate OAS-compliant documentation, or string together a series of calls to generate a full OAS document for multiple API endpoints.

What is the annotation required to enable the Swagger?

The @EnableSwagger2 annotation is used to enable the Swagger2 for your Spring Boot application.

Is REST API documentation template available on Swagger?

The open source Swagger framework helps remedy these issues for API consumers and developers. The framework provides the OpenAPI Specification (formerly known as the Swagger specification) for creating RESTful API documentation formatted in JSON or YAML, a human-friendly superset of JSON.

How do I enable swagger UI in web API?

To add Swagger to your ASP.NET Web API project, you need to install an open-source project called Swashbuckle via NuGet as shown below. Once the package is installed successfully, navigate to the App_Start folder in Solution Explorer. You will find a new file called SwaggerConfig. cs.


1 Answers

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.

like image 185
Ron Avatar answered Nov 09 '22 04:11

Ron