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?


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


I solved my problem by adding swagger-ui resources to

srcmainwebappdocs

I also added

<mvc:default-servlet-handler/>

To pom.xml

Now I can access swagger UI under:

http://localhost:8080/myapp/docs/
链接地址: http://www.djcxy.com/p/82626.html

上一篇: 闪亮的情节Google地图Internet Explorer与Chrome

下一篇: 如何启用Swagger UI? Tomcat,SpringMVC,REST