Swagger UI not working Rest API Java

1,827 views
Skip to first unread message

Aniruddh Tiwari

unread,
May 5, 2016, 1:13:21 PM5/5/16
to Swagger

I have a Rest Api project and I am trying to use swagger to documentation and UI. I was able to get the documentation but UI is not seeing api documentation and pulls up blank. I have copied contents of dist folder under webapps/WEB-INF I can access the documentation using:http://localhost:8080/ml-api-ws/v2/api-docs I tried to change the url in index.html to point to this documentation but was swagger pulls up blank.

webapps/WEB-INF:

enter image description here

Blank Swagger:

enter image description here 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";
        url = "http://localhost:8080/ml-api-ws/v2/api-docs";
      }

Pom.xml

<!-- SpringFox Dependency For Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.4.0</version>
</dependency>
<!-- Swagger UI Depedency -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.0.2</version>
</dependency>

servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<context:annotation-config/>
    <context:component-scan base-package="com.epsilon.al.ml.controller" />

<mvc:resources  mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources  mapping="/webjars/**" location="classpath:/META-INF/webjars/"/>
<mvc:default-servlet-handler/>
    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
         <value>/WEB-INF/jsp/</value>       
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

<bean id="swagger2Config" class = "springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" />
    <!-- <bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" 
        /> <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" 
        /> -->
</beans>

Web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>MlApi</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>      
        <param-value>classpath:*-context.xml</param-value>
    </context-param> 

  <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>ml-rest-api</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ml-rest-api</servlet-name>
        <!-- <url-pattern>/ml-api-ws/*</url-pattern> -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

<!--    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list> -->
        <welcome-file-list>
        <welcome-file>/WEB-INF/index.html</welcome-file>
    </welcome-file-list>
</web-app>

I also tried saving the the api documentation in .json file and explore with swagger-ui but the page did not do anything.

Any Direction or help would be great, thanks!!

tony tam

unread,
May 6, 2016, 12:50:33 PM5/6/16
to swagger-sw...@googlegroups.com
Hi, the static files should go in the webapp folder, not the WEB-INF folder.  Can you try that?

--
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.

Ron Ratovsky

unread,
May 6, 2016, 2:27:53 PM5/6/16
to swagger-sw...@googlegroups.com
We should have added a note yesterday. Ani and I went over it yesterday on IRC and resolved the issue.
The solution was indeed to move it to the webapp folder, and add another routing to expose it.

Aniruddh Tiwari

unread,
May 6, 2016, 3:49:33 PM5/6/16
to Swagger
I also have to add mapping for index.html in servlet.xml. After that it worked just fine.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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-swaggersocket+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages