Gavin Fitzgerald
Gavin Fitzgerald

Reputation: 396

Jersey JAX-RS, Swagger - getting swagger.json generated but no UI

I'm having an issue loading the Swagger UI / enabling the UI endpoint. Maven project, Jersey version - 2.12, Swagger version - 1.5.1-M2

I've a programmatically configured jersey web app. In my (extension of) ResourceConfig, I set the following for Swagger:

    beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.0");
    beanConfig.setHost("http:localhost:8080");
    beanConfig.setBasePath("/app/v1");
    beanConfig.setResourcePackage("com.app.features");
    beanConfig.setScan(true);
    

    register(beanConfig);

    register(new ApiListingResourceJSON());
    register(new SwaggerSerializers());

I also have a bootscrap class, which I load via web.xml :

public class Bootstrap extends HttpServlet {

  @Override
  public void init(ServletConfig config) throws ServletException {
    Info info = new Info()
            .title("Swagger Sample App")
            .description("Desc")
            .termsOfService("http://helloreverb.com/terms/")
            .contact(new Contact()
                    .email("[email protected]"))
            .license(new License()
                    .name("Apache 2.0")
                    .url("http://www.apache.org/licenses/LICENSE-2.0.html"));

    ServletContext context = config.getServletContext();
    Swagger swagger = new Swagger().info(info);
    context.setAttribute("swagger", swagger);
  }
}

Said web.xml:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Bootstrap</servlet-name>
    <servlet-class>com.app.Bootstrap</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

I have copied the contents of the Swagger UI dist to my web app folder.

When I hit the api json endpoints on http://localhost:8080/app/v1/swagger.json I do get the json code, e.g. :

{"swagger":"2.0","info":{"version":"1.0.0"},"host":"http:localhost:8080","basePath":"/app/v1"}

but I don't seem to see the Swagger UI on the paths I would expect (http:localhost:8080/app/v1 or http:localhost:8080/app/v1/app/v1/index.html).

I'm unfortunately not as comfortable with Jersey as I am with Spring, so any assistance would be welcome.

Thanks

Upvotes: 1

Views: 6961

Answers (4)

vaquar khan
vaquar khan

Reputation: 11489

Please find following working example

package com.vk.test.swagger;

import static springfox.documentation.builders.PathSelectors.regex;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2

/**
 * 
 * @author vaquar khan
 *
 */
public class SwaggerConfiguration {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("com.vk.test.controller")).paths(regex("/api/apiPath.*"))
                .build();

    }

}

Maven

   <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>
        <!-- Swagger UI -->

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>

Call swagger

http://<servername>:<Port>/swagger-ui.html

Upvotes: 0

ootero
ootero

Reputation: 3475

There is a typo in:

beanConfig.setHost("http:localhost:8080");

should be:

beanConfig.setHost("http://localhost:8080");

If you are using Maven, could you try including the swagger-uidependency:

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

or a more recent version.

and try opening this url in a browser:

http://localhost:8080/app/v1/api-docs?url=/app/v1/swagger.json

Upvotes: 0

Ron
Ron

Reputation: 14850

We're covering it also in the google group, but it's most likely due to your resources not being directly under the com.app.features package but rather a sub-package such as com.app.features.foo. If they span over multiple packages you can set those as com.app.features.foo,com.app.features.bar.

I believe the deep scanning has been changed in 1.5.2-M2, so you can try that as well.

Upvotes: 0

chengpohi
chengpohi

Reputation: 14227

You need to download swagger-ui web project, and put it in your web directory, and config your api address in index.html,

index.html:

  <script type="text/javascript">
    $(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"; //your api address
      }
      window.swaggerUi = new SwaggerUi({
        url: url,

and now you can visit the swagger-ui by visiting http://ip:port/path/swagger-ui/index.html.

Upvotes: 0

Related Questions