I have an application that uses Spring MVC to handle REST calls, the Controllers are REST Controllers and annotated with @RestController
, and the controller and its methods are annotated with @RequestMapping
.
I'm trying to add Swagger to generate documentation for the existing REST services. I'm trying to add it for one as a test to see what it looks like. I've added the springfox
and ui
libraries, added swagger config classes and bean definitions to the spring context xml
file. I can hit the swagger-ui.html
link, but it's not generating any documentation at all. Because of the sparse documentation, I have no idea what I'm doing wrong or misconfigured, and Googling the issue doesn't produce anything useful.
Can anyone provide some suggestions of what I might have done wrong, or alternatively, some better documentation than I've found so far?
I added springfox-swagger2 and the associated ui entry to my pom file, added this: to my spring file, along with two new bean definitions for these classes:
@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
@EnableWebMvc
public class SwaggerConfiguration 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/");
}
}
I've tried defining the beans in the second class in my spring xml, but that didn't work either. I'm stumped. I've done what the documentation said to do, and it's not working. Any ideas?
The springfox documentation is available here. From your post it appears that you may need to do the following
NOTE: this is not needed if you have spring boot application.
http(s)://your-host/context-path/v2/api-docs