Unable to get Swagger UI working with Spring boot
If you are experiencing this issue with version springfox swagger-ui 3.x or greater..
try the below url.. It worked for me..
http://localhost:8080/swagger-ui/
For complete swagger documentations steps, refer: http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/
I have the same issue but this link works for me: http://localhost:8080/sdoc.jsp
It pre-populates the swagger ui resource url box with : http://localhost:8080${pageContext.request.contextPath}/api-docs
and when I hand edit it removing ${pageContext.request.contextPath} and hit Explore it shows me my api doc and I can even try my endpoints successfully. So definitely an issue but probably not picking up ${pageContext.request/contextPath}.
Looking at the source the javascript has: url: window.location.origin + "${pageContext.request.contextPath}/api-docs"
on a static swagger ui html I have this piece is coded as:
discoveryUrl:"./resource-list.json"
I hope this is a bit helpful
Your problem lies in your SwaggerConfiguration
file. You need to take out @EnableWebMvc
, because this causes the default Spring Boot view resolver to be overwritten by the default 'SpringWebMvc' one which serves static content differently.
By default, Spring Boot will serve static content from any of the following directories:
- /META-INF/resources/
- /resources/
- /static/
- /public/
including webjars.
I had the same problem and I found this in the documentation: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
If you want to take complete control of Spring MVC, you can add your own
@Configuration
annotated with@EnableWebMvc
. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own@Bean
of typeWebMvcConfigurerAdapter
, but without@EnableWebMvc
.
I hope this helps.
I have swagger-ui v0.4 (with spring v4.14 & swagger-springmvc v0.9.4) working fine, although I had some similar problems at first. It seems like this class does the trick.
@Configuration
@EnableSwagger
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Autowired
private SpringSwaggerConfig springSwaggerConfig;
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(springSwaggerConfig).apiInfo(
apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(/* strings */);
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
I believe the relevant thing is the overridden configureDefaultServletHandling
. And on my main WebApplicationInitializer
, I have:
@Import(SwaggerConfig.class)
Finally, I fixed the issue with the UI's location box showing "http://localhost:8080${pageContext.request.contextPath}/api-docs" by including this in my dependencies:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<version>8.0.15</version>-->
<scope>provided</scope>
</dependency>
That provides something related to JSP processing. It is included in the dependencies of spring-boot
, but it isn't normally provided
.
Hope that helps.