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.