10

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?

Dariusz Mydlarz
  • 2,940
  • 6
  • 31
  • 59
  • Have you tried the context root of your application? ttp://localhost:8080/myap? or ttp://localhost:8080/myapp/index.html? – Ron Nov 25 '14 at 17:18
  • I implemented a demo, but I didn't use webjar (I copied UI resources). You can check it out here: https://github.com/jxc876/boot-swagger-demo Hope it helps. – Mike R Nov 25 '14 at 17:20
  • Under `src\main\webapp\WEB-INF` I have got Spring Web application with `web.xml` configuration file. I put swagger-ui resources into `src\main\webapp\docs`. What should I add to web.xml to serve also that content? Under `http://localhost:8080/myapp/` I have my web app. I dont know where is swagger UI – Dariusz Mydlarz Nov 26 '14 at 08:01
  • @Mike R, The boot-swagger-demo does not work. The Hello world displays but the /api-docs does not – cosbor11 Jun 23 '15 at 00:44
  • I use Springfox Swagger UI. You can see my answer here http://stackoverflow.com/a/33258645/3608312 – Yuriy Tumakha Oct 21 '15 at 12:04
  • I did exact same configuration, but it's not working for me. – PAA Dec 23 '15 at 11:05

2 Answers2

5

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

leo
  • 3,677
  • 7
  • 34
  • 46
1

I solved my problem by adding swagger-ui resources to

src\main\webapp\docs

I also added

<mvc:default-servlet-handler/>

To pom.xml

Now I can access swagger UI under:

http://localhost:8080/myapp/docs/
Dariusz Mydlarz
  • 2,940
  • 6
  • 31
  • 59