How to view Swagger UI docs from swagger.json in JBoss

2,765 views
Skip to first unread message

Derek Scotney

unread,
Feb 6, 2017, 7:44:08 AM2/6/17
to Swagger
Hi

I know this topic (or similar) seems to have been raised in many forums. I have tried so many different "solutions", but nothing is working for me.  The scenario is that I have created a swagger.json file describing my RESTful API and using the "swagger-codegen-maven-plugin", I have generated the java code (maven setup below). This worked flawlessly. 

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
    <goals>
        <goal>generate</goal>
    </goals>
    <configuration>
        <inputSpec>${project.basedir}/src/main/webapp/swagger.json</inputSpec>
<language>jaxrs-resteasy</language>
<output>${project.build.directory}/generated-sources</output>
<configOptions>
    <sourceFolder>src/main/java</sourceFolder>
    <invokerPackage>za.co.company.ws.rest</invokerPackage>
    <apiPackage>za.co.company.ws.rest.api</apiPackage>
    <modelPackage>za.co.company.ws.rest.model</modelPackage>
</configOptions>
    </configuration>
</execution>
    </executions>
</plugin>

I also have the following dependencies:
- "swagger-jaxrs" v1.5.12
- "swagger-ui" v2.2.10 (I'm not sure if this necessary, but anyway)

I can build this code, deploy it in JBoss and access the endpoints with the auto-generated responses. PERFECT.

I would now like to view the Swagger UI docs for the API (i.e. like with the sample PetStore app). To use use swagger-ui, I have followed the installation method of copying the contents of the "dist" directory of the swagger-ui project into my project src/main/webapp/swagger directory, and modify the line in the index.html file "url = "http://petstore.swagger.io/v2/swagger.json";" to reflect my own path to the swagger.json file (url = "http://localhost:8080/rest/swagger.json";).  My swagger.json resides in the src/main/webapp.

My web.xml file contains (as per information obtained from https://github.com/swagger-api/swagger-core/wiki/1.3--1.5-Migration ) 

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

  <display-name>RESTful Webservice</display-name>

  <context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>
      io.swagger.jaxrs.listing.ApiListingResource,
      io.swagger.jaxrs.listing.AcceptHeaderApiListingResource
    </param-value>
  </context-param>
  
  <context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>io.swagger.jaxrs.listing.SwaggerSerializers</param-value>
  </context-param> 
  
  <servlet>
    <servlet-name>DefaultJaxrsConfig</servlet-name>
    <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
    <init-param>
      <param-name>api.version</param-name>
      <param-value>1.0.0</param-value>
    </init-param>
    <init-param>
      <param-name>swagger.api.title</param-name>
      <param-value>RESTful Webservice</param-value>
    </init-param>    
    <init-param>
      <param-name>swagger.api.basepath</param-name>
      <param-value>http://localhost:8080/Srest</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
 
  <filter>
    <filter-name>ApiOriginFilter</filter-name>
    <filter-class>za.co.smartcall.ws.rest.api.ApiOriginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>ApiOriginFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

With this configuration, I can call each endpoint of the generated rest service and get the default responses (under http://localhost:8080/rest/<endpoint>).

If I call http://localhost:8080/rest/swagger.json, instead of seeing the content of my swagger.json file, all I see returned is:

{"swagger":"2.0","info":{"version":"1.0.0","title":"RESTful Webservice"},"host":"localhost:8080","basePath":"/rest","schemes":["http"]}

which is what is configured in the DefaultJaxrsConfig servlet in my web.xml.


If I call http://localhost:8080/rest/swagger, a file is downloaded containing the following (same content as above):

---
swagger: "2.0"
info:
  version: "1.0.0"
  title: "RESTful Webservice"
host: "localhost:8080"
basePath: "/rest"
schemes:
- "http"

If I try http://localhost:8080/rest/swagger/index.html, I just get a JBoss 404 error, "JBWEB000124: The requested resource is not available.".

If I load the index.html file in my browser (src/main/webapp/swagger/index.html), I get a swagger-ui formatted page with no content from my swagger.json file.  

I know I must be missing something simple, but it's damned hard to find :)

I would be immensely grateful for an assistance.




tony tam

unread,
Feb 6, 2017, 3:18:49 PM2/6/17
to swagger-sw...@googlegroups.com
Try adding your class package to this section in your web.xml:

  <context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>
      io.swagger.jaxrs.listing.ApiListingResource,
      io.swagger.jaxrs.listing.AcceptHeaderApiListingResource
    </param-value>
  </context-param>

That’s where the swagger framework looks for classes to scan.


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

calde...@yahoo.com

unread,
Feb 6, 2017, 8:54:40 PM2/6/17
to swagger-sw...@googlegroups.com

--------------------------------------------
On Mon, 2/6/17, tony tam <feh...@gmail.com> wrote:

Subject: Re: How to view Swagger UI docs from swagger.json in JBoss
To: swagger-sw...@googlegroups.com
Date: Monday, February 6, 2017, 10:18 PM
n a doua jumatate a anului 1838 Alexandru Ghica a plecat din tara tentia declarata de a-si ingriji sanatatea in strainatate. in absenta sa din se anunta la 20 ianuarie 1839 ca o parte dintre boieri sunt hotarati sa-l ja domn pe Ion Campineanu candidatura insa repede abandonata in lipsa imitatii.

Derek Scotney

unread,
Feb 7, 2017, 4:29:37 AM2/7/17
to Swagger
Hi

I added all my generated class files, however I got the same result.  I know that the class files were parsed as I accidentally added some files without the @Path and the deployment broke when these files were read.

{"swagger":"2.0","info":{"version":"1.0.0","title":"RESTful Webservice"},"host":"localhost:8080","basePath":"/rest","schemes":["http"]}

Since I already have a pre-built swagger.json file (from which the api and model is generated), is it not possible for this to be used for the swagger-ui docs?  Having said that, I willing to implement whatever works (other than using Spring) :)
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Derek Scotney

unread,
Feb 8, 2017, 6:20:36 AM2/8/17
to Swagger
It looks like the problem arises from the fact that when Swagger Codegen creates the API java files from the swagger.json file, it doesn't include the necessary @Api annotations.  It seems insane to me that I need to edit the auto-generated classes to insert annotations to make Swagger work.

Derek Scotney

unread,
Feb 8, 2017, 8:11:31 AM2/8/17
to Swagger
I see this issue is being addressed in https://github.com/swagger-api/swagger-codegen/issues/4447

Looks like the fix will be in codegen v2.2.2.
Reply all
Reply to author
Forward
0 new messages