SwaggerUI作用:
1.所有接口方法可以动态的生成API文档,开发无需手动编写文档
2.研发可以直接点击对应接口完成自测
3.测试人员可以测试。

  • pom.xml文件添加依赖
<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.7.0</version>
</dependency>

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.7.0</version>
</dependency>
  • 创建swagger配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .useDefaultResponseMessage(false)
                .select()
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Swagger 接口文档")
                .contact(new Contact("swagger","","swagger@swagger.com.cn"))
                .description("Swagger 生成的接口文档")
                .version("1.0.0.0")
                .build();

    }
}

我们在使用SpringBoot集成Swagger2中,访问:http://localhost/swagger-ui.html 如果出现问题,页面显示默认报错页面。后台报错:

No mapping found for HTTP request with URI [/swagger-ui.html] in DispatcherServlet with name ‘dispatcherServlet’

解决方案

这个错误,是因为静态资源路径映射问题导致。
我们在访问 http://localhost/swagger-ui.html 时,这个swagger-ui.html 相关的所有前端静态文件都在 springfox-swagger-ui-2.7.0.jar 里面。SpringBoot自动配置本身并不会把 /swagger-ui.html 这个路径映射到对应的目录 META-INF/resources/下面。我们加上这个映射即可。构建一个WebMvcConfig 类,代码如下:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebMvcConfig 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/");
    }
}

或者直接在 SwaggerConfig 类上继承 WebMvcConfigurerAdapter ,代码如下:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .useDefaultResponseMessage(false)
                .select()
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Swagger 接口文档")
                .contact(new Contact("swagger","","swagger@swagger.com.cn"))
                .description("Swagger 生成的接口文档")
                .version("1.0.0")
                .build();

    }
}

需要特别说明一下的是,SwaggerConfig 这个类一定要配置正确。