I am trying to get Swagger UI working with Spring Boot 1.2.1. I followed the instructions at https://github.com/martypitt/swagger-springmvc and I added @EnableSwagger
on my spring config.
I currently get back JSON when I go to http://localhost:8080/api-docs
but no nice HTML.
I am using Maven and added the dependency on swagger-ui:
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
This is my complete list of dependencies:
<dependencies>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>
</dependency>
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I also tried http://localhost:8080/docs/index.html
as URL, but that just gives the "Whitelabel Error Page"
Update:
I created a test project on Github to show the problem: https://github.com/wimdeblauwe/springboot-swagger-test
To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.
Go to http://localhost:8000/ in your address bar. This address lets you view the local web server. By default, web servers default to the index. html file in the directory, so it will show the Swagger UI file automatically.
Once your application is started, you can go to http://localhost:8080/q/swagger-ui and play with your API.
Enabling Springfox's Swagger UI You need to add one additional dependency to the pom. xml to use Swagger UI. Now you can get the Swagger UI using http://localhost:8080/swagger-ui.html#/.
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With