Swagger UI does not render content on index.html page

UPDATED SOLUTION below !!!
//////////////////////////////////////////////// ////////////
Thanks to Ron's advice below, I modified my setup a bit to use BeanConfig instead of SwaggerConfig and got this working. To do this, I had to modify the servlet and also (and here I believe the missing part was) added a BeanConfig entry to the spring application context file for spring to collect resources. I have included the updates below with comments in my code showing the old and new / updated code. Maybe I could continue with SwaggerConfig (maybe I just missed something in the spring app context file), but the BeanConfig is working, so I'm going to leave it as it is. //////////////////////////////////////////////// ////////////

I have been trying to get Swagger running with my local REST based Java application and have had considerable success. However, I seem to be missing something when I try to get the Swagger interface to work.

Whenever I actually get to this address: http: // localhost: 9082 / mbl / index.html I get a little green swagger header on top, but an empty white body with no content. Shouldn't I be seeing more than this in the body of the page?

My stack is Java 6 / Wink / spring 3.1 / Jackson 2.5 / JAX-RS (JSR-311) and I am using the following Swagger banners: swagger-annotations-1.3.10.jar / swagger -core_2.10-1.3.10. jar / swagger-jaxrs_2.10-1.3.10.jar.

Now I have managed to display json which looks like this when I find http: // localhost: 9082 / mbl / services / api-docs :

{"apiVersion":"1.0","swaggerVersion":"1.2","info":{"title":"Java API","description":"The following documentation contains the REST Service API useful for interacting with web services.","termsOfServiceUrl":"terms of service","contact":"email@test.com","license":"license type","licenseUrl":"license url"}}

      

I can see that this is generated from my SwaggerServlet.java which looks like this:

package com.somewhere.mblsvc.web;

import...

public class SwaggerServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /* additional working code */
    BeanConfig beanConfig;

    public void setBeanConfig(BeanConfig beanConfig) {
        this.beanConfig = beanConfig;
    }
    /* end additional working code */    

    @Override
    public void init(ServletConfig servletConfig) {
        try {
        /* code from original post*/
        //  SwaggerConfig swaggerConfig = new SwaggerConfig();
        //  ConfigFactory.setConfig(swaggerConfig);
        //  swaggerConfig.setBasePath("/mbl/services");
        //  swaggerConfig.setApiVersion("1.0");
        //  swaggerConfig.setApiInfo(new ApiInfo("Java API", "The following //documentation contains the REST Service API useful for interacting with web //services.", "terms of service", "email@test.com", "license type", "license //url"));
        //  ScannerFactory.setScanner(new DefaultJaxrsScanner());
        //  ClassReaders.setReader(new DefaultJaxrsApiReader());
        /* end code from original post*/

        /* updated working code */
        beanConfig.setBasePath("/mbl/x-services");
        beanConfig.setVersion("1.0");
        beanConfig.setResourcePackage("com.somewhere.mblsvc.resources");
        beanConfig.setScan(true);
        /* end updated working code */

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

      

Also, my spring application xml application file has the following:

<bean class="org.apache.wink.spring.Registrar">
    <property name="classes">
        <set value-type="java.lang.Class">
        </set>
    </property>
    <property name="instances">
        <set>
            <ref local="jaxbProvider" />
            <ref local="apiDeclarationProvider" />
            <ref local="apiListingResourceJson" />
            <ref local="resourceListingProvider" />
        </set>
    </property>

<!-- Jackson Providers -->
<bean id="jaxbProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" >
    <property name="mapper" ref="jacksonObjectMapper"/>
</bean>

<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" >
    <property name="annotationIntrospector" ref="jacksonAnnotationIntrospector" />
</bean>

<bean id="jacksonAnnotationIntrospector" class="com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair" >
    <constructor-arg ref="primaryAnnotationIntrospector" />
    <constructor-arg ref="secondaryAnnotationIntrospector" />
</bean>

<bean id="primaryAnnotationIntrospector" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />
<bean id="secondaryAnnotationIntrospector" class="com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector" />

<!-- Swagger Configuration and Providers -->

<!-- additional working code -->
<bean id="beanConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
    <property name="title" value="Java API"/>
    <property name="version" value="1.0" />
    <property name="basePath" value="/mbl/services"/>
    <property name="resourcePackage" value="com.somewhere.mblsvc.resources"/>
    <property name="scan" value="true"/>
</bean>
<!-- end additional working code -->

<bean id="apiDeclarationProvider" class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider" />
<bean id="apiListingResourceJson" class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON" />
<bean id="resourceListingProvider" class="com.wordnik.swagger.jaxrs.listing.ResourceListingProvider" />

      

My web.xml looks like this:

<!-- REST servlet that dispatches to the App (resource class). Remove Init params entry containing application file -->
<servlet>
    <servlet-name>Wink Servlet</servlet-name>
    <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Wink Servlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

<!-- Enabling Swagger servlet -->
<servlet>
    <servlet-name>Swagger Servlet</servlet-name>
    <servlet-class>com.somewhere.mblsvc.web.SwaggerServlet</servlet-class>
    <load-on-startup>-1</load-on-startup> 
</servlet>
<servlet-mapping>
    <servlet-name>Swagger Servlet</servlet-name>
    <url-pattern>/api-docs</url-pattern>
</servlet-mapping>

      

Here is a snippet of my resource class:

@Path("test")
@Api(value ="test", description="Test Services")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class TestResource {

.
.
.

    @GET
    @Path("testInfo")
    @ApiParam(defaultValue="you would put test case input here for a post")  
    @ApiOperation(value="Composite service returning extensive test information", response=com.somewhere.mblsvc.messages.test.testinfo.pojo.UserResponseMessage.class)
    @ApiResponses(value={
            @ApiResponse(code=200, message="OK"),
            @ApiResponse(code=500, message="Internal Error")
    })
    @JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS)
    public Response getTestInfo(@Context HttpHeaders headers, 
            @CookieParam(value = "testBrand") String testBrand) {
.
.
.

      

And finally, the only important part of my index.html (which I can tell) looks like this:

  <script type="text/javascript">
    $(function () {
      var url = window.location.search.match(/url=([^&]+)/);
      if (url && url.length > 1) {
        url = url[1];
      } else {
        url = "http://" + window.location.hostname + (window.location.port ? ':'+ window.location.port: '') + "/mbl/services/api-docs";
      }
      .
      .
      .

      

I will gladly provide additional information as needed. Does anyone know what I am missing?

Thank you so much!

+3


source to share


1 answer


The main problem is that your resources are not being checked. Now you get right to Swagger's main answer, but if you look at the content, there are no API definitions in it.

As a result, swagger-ui cannot show anything, because nothing is visible.

While I'm wondering how you got to the configuration above, the truth is that the integration could be easier. We don't have any specific documentation for Wink (we should), but the idea is very similar to any JAX-RS integration.



I would recommend the following steps:

  • Port to swagger 1.5 kernel. Obviously this is a new integration and there is no reason not to use it.
  • Pay attention to the pattern - it should be very easy to follow.
  • Read the guides for 1.5 - they are not for a wink, but they are all similar, and you can infer to wink.
  • Check the migration guide as it also contains details that may help you.

I realize this does not address the issue exactly, but I would rather promote the correct generic solution in this case.

+1


source







All Articles