Swagger 2.0 for Spring MVC not working

2019-02-26 22:03发布

问题:

I have some REST APIs written using Spring MVC. I am trying to implement Swagger2.0 into this. I am not using Spring Boot and I am also not using the swagger-ui dependency. My aim is to generate the docs of my APIs in JSON format.

I referenced the following links: https://github.com/springfox/springfox-demos/tree/master/spring-java-swagger and A 'simple' way to implement Swagger in a Spring MVC application

However, I am unable to see the docs when I visit http://localhost:8080/v2/api-docs. I believe I am missing something really simple, especially with my Spring configuration file.

I have the following dependency in my pom.xml file:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
</dependency>

SwaggerConfig.java

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
public class SwaggerConfig {

}

WebConfig.java

/* import statements */

@Configuration
@ComponentScan(basePackages = {"com.sample.controller"})
@EnableWebMvc
@Import(SwaggerConfig.class)
public class WebConfig extends WebMvcConfigurerAdapter{

    public WebConfig(){
        super();
    }

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}

Spring configuration XML contains the following:

<context:component-scan base-package="com.sample.controller" />
<mvc:annotation-driven />

Any help would be appreciated. Again, I am not planning on using Spring Boot and I would be configuring swagger-ui once I get this working.

Thanks in advance!