I've been trying to add Springfox-Swagger UI 3.0.0 into a regular Spring MVC application with Spring Security (non-spring-boot) and cannot figure it out. I recently incorporated swagger-ui into 2 spring-boot projects which were much easier without any issue.
I've followed numerous tutorials for this and none of them are working.
The latest tutorial I followed is the official one by SpringFox I also followed the demo projects from https://github.com/springfox/springfox-demos and more specifically the spring-xml-swagger demo
Requests for /swagger-ui/,/swagger-ui/index.html, /swagger-ui.html and /v2/api-docs/ return 404.
I've also tried with older versions of swagger-ui but none of them worked. Since it's a company project I can't reveal too much but any suggestions would be nice. I'll try to update this post with some configuration code if necessary.
Dependencies from pom.xml
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
Spring Framework is 5.2.x
My SwaggerConfig.class.
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Api Services")
.description("Api Services")
.version("v1")
.build();
}
@Bean
public UiConfiguration uiConfiguration() {
return UiConfigurationBuilder
.builder()
.defaultModelsExpandDepth(-1)
.build();
}
}
From the xml config.
<mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
<mvc:view-controller path="/swagger-ui/" view-name="forward:/swagger-ui/index.html"/>
<mvc:resources mapping="index.html" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
<bean id="swaggerConfig" class="com.example.config.SwaggerConfig"/>
Solved it.
For anyone interested, it turns out that I needed to configure Servlet configuration through web.xml since our current servlet was configured with
<url-pattern>*.do</url-pattern>
and that was blocking me from accessing the swagger related URLs.
The configuration which solved my issue is the following.
web.xml
<servlet>
<servlet-name>swagger-ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/swagger-servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>swagger-ui</servlet-name>
<url-pattern>/swagger-ui/index.html</url-pattern>
<url-pattern>/swagger-ui/springfox.css</url-pattern>
<url-pattern>/swagger-ui/swagger-ui.css</url-pattern>
<url-pattern>/swagger-ui/springfox.js</url-pattern>
<url-pattern>/swagger-ui/swagger-ui.js</url-pattern>
<url-pattern>/swagger-ui/swagger-ui-bundle.js</url-pattern>
<url-pattern>/swagger-ui/swagger-ui-standalone-preset.js</url-pattern>
<url-pattern>/swagger-ui/favicon-32x32.png</url-pattern>
<url-pattern>/swagger-ui/favicon-16x16.png</url-pattern>
<url-pattern>/swagger-resources/configuration/ui</url-pattern>
<url-pattern>/swagger-resources/configuration/security</url-pattern>
<url-pattern>/swagger-resources</url-pattern>
<url-pattern>/v2/api-docs</url-pattern>
</servlet-mapping>
swagger-servlet-context.xml
<context:component-scan base-package="my.app.path.config, my.app.path.web.controllers" />
<!-- cache resources to the browser-->
<mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/swagger-ui/**" />
<mvc:mapping path="/v2/api-docs/**" />
</mvc:interceptor>
</mvc:interceptors>
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven/>