Reputation: 26848
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
Upvotes: 25
Views: 67603
Reputation: 1821
I have faced same issues in my project. resolved issue with below steps.
Added below dependencies in pom.xml
<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Added swagger2 ui configuration as below in the project level
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Then able to get the swagger ui documentation http://localhost:8080/swagger-ui/
swagger api docs http://localhost:8080/v2/api-docs
Upvotes: 0
Reputation: 331
As indicated by Tamas above, the problem lies in using @EnableWebMvc, which goes around the default setup and skips some things Swagger needs. Switching that to @EnableSwagger2 for me was enough to fix the issues in my project, which showed similar symptoms.
Upvotes: 0
Reputation: 2336
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/
Upvotes: 3
Reputation: 1348
I have done separate config for Swagger and my problem was that without @EnableAutoConfiguration
it was not working properly.
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Upvotes: -2
Reputation: 1662
I would suggest you to use @EnableSwagger2 tag and follow the steps and code from here: https://github.com/sanketsw/SpringBoot_REST_API
Also i am using following dependency which works perfectly fine:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
Upvotes: -3
Reputation: 546
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.
Upvotes: 38
Reputation: 1089
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.
Upvotes: 4
Reputation: 11
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
Upvotes: 1