Needless to say I know there are a ton of questions about this same subject but I've been stuck trying to fix this for 2 days now and most of what I've read is outdated.
So yeah.. this is what I have right now:
Gradle:
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-hystrix'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.ws:spring-ws-core'
compile 'io.springfox:springfox-swagger2:2.4.0'
compile 'io.springfox:springfox-swagger-ui:2.4.0'
}
Main class:
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(FeedServiceApplication.class, args);
}
@Bean
public Docket swaggerSettings() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/");
}
When I visit: http://localhost:8080/v2/api-docs, I get the json documentation just fine. Also when I download swagger-ui from github, set the source to the link above and run it on desktop, it also works fine. What doesn't work is putting these 2 things together (getting it to work at http://localhost:8080/swagger-ui.html).
There are many tutorials such as these, where they claim the stuff above will make swagger-ui work:
http://kubecloud.io/guide-using-swagger-for-documenting-your-spring-boot-rest-api/
http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
And a ton of other tutorials where they instruct you to add dist folder from swagger-ui git to your project.
Also mapping like this:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("**/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("**/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
Fails as well, throwing Scope 'request' is not active for the current thread; exception.
After trying some tutorials from youtube, the ones linked above and many others, all I've seen is "page not found". If anyone could explain what I'm missing I would be very grateful.
TL:DR How do I get swagger-ui.html to work?
EDIT: found the solution.
In case anyone else stumbles upon this, the problem is that if you have a request mapping that takes in a parameter @RequestMapping("/{param}")
, dispatcherServlet no longer maps /** to ResourceHttpRequestHandler. The code below fixes that issue.
@Configuration
@EnableAutoConfiguration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}