0
votes

I have some problems trying to start with SwaggerUI because Swagger doesn't generate the "swagger.json" file

I'm working with Eclipse and Tomcat 8, I downloaded the definition from here and I followed the instructions from

Swagger-Core-Jersey-2.X-Project-Setup-1.5

As I understand there are 3 ways to configure swagger in your local repository based on your JERSEY configuration I followed this one "Using Jersey 2 container Servlet or Filter (with web.xml)" and these are the steps that I made for the project

1.- Created a Jersey Project using this Maven archetype

mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.22.1

2.- Added this SwaggerUI dependency to my POM file

        <dependency>
          <groupId>io.swagger</groupId>
          <artifactId>swagger-jersey2-jaxrs</artifactId>
          <version>1.5.0</version>
        </dependency>

3.- I Modified the Web.xml file, to add the Swagger "param-value" (Notice that my resource is in the package com.api, and the "url-pattern" value is "api" ) folowing the instructions from

package-scanning--concrete-class-selection

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,com.api</param-value>          
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

4.- And finally I added the Swagger Servlet into the Web.xml file, here is the full xml file (Notice the "swagger.api.basepath" value)

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.swagger.jaxrs.listing,com.api</param-value>          
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Jersey2Config</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <init-param>            
            <param-name>swagger.api.basepath</param-name>
            <param-value>http://localhost:8080/SwaggerTest1/api/</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

</web-app>

Acordingly to that guide this will generate a swagger.json file but It doesn't exist in my project. there was a problem when I'm trying to give the path for swagger.api.basepath value ? or maybe there is a problem with the "jersey.config.server.provider.packages" value ?

Additionals.

Here is MyResource.java class

package com.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;



/**
 * Root resource (exposed at "myresource" path)
 */
@Path("/myresource")
public class MyResource {

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

Project files

I have several days trying to solve this so Thanks for your help !

1

1 Answers

0
votes

UPDATE!

I noticed that there are no "swagger.json" file. Swagger creates it at compile time.

Based on that I changed the Swagger Servlet configuration like this

<servlet>
        <servlet-name>Jersey2Config</servlet-name>
        <servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
        <init-param>
            <param-name>api.version</param-name>
            <param-value>1.0</param-value>
        </init-param>
        <init-param>            
            <param-name>swagger.api.basepath</param-name>
            **<param-value>http://localhost:8080/SwaggerTest1/api</param-value>**
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

and in the file.html (inside the dist folder) change the url value as follows

$(function () {
      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = decodeURIComponent(url[1]);
      } else {
        url = "http://localhost:8080/SwaggerTest1/api/swagger.json";
      }

with this changes I finally open Swagger with this URL

http://localhost:8080/SwaggerTest1/dist