Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Swagger UI? Tomcat, SpringMVC, REST

My configuration is as follow:

pom.xml

<dependencies>
        <!-- Swagger -->
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- Swagger WebJar -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>swagger-ui</artifactId>
            <version>2.0.24</version>
        </dependency>
</dependencies>

root-context.xml

<mvc:annotation-driven/>
<beans:bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />

I deploy my application into Tomcat 8.0. I am able to see Swagger JSON data at URI:

http://localhost:8080/myapp/api-docs

But I can't run Swagger UI. What more should I do to run Swagger UI in my project?

like image 915
Dariusz Mydlarz Avatar asked Nov 25 '14 17:11

Dariusz Mydlarz


1 Answers

The trick is that your swagger-ui maven dependency is a webjar. You need to configure the path from your webserver to the webjar.

I used the current maven dependency:

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

And configured it with annotations:

@Configuration
@EnableWebMvc
public class RestConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/2.1.3/");
    }
// [...]
}

Same can be done with with xml in your spring configuration:

<mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/swagger-ui/2.1.3/"/>

Also see: http://www.jamesward.com/2012/04/30/webjars-in-spring-mvc

like image 191
leo Avatar answered Oct 05 '22 03:10

leo