SpringBoot集成Swagger2

SwaggerUI做用:
1.全部接口方法能夠動態的生成API文檔,開發無需手動編寫文檔
2.研發能夠直接點擊對應接口完成自測
3.測試人員能夠測試。html

  • 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 類,代碼以下:web

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 ,代碼以下:spring

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 這個類必定要配置正確。api

相關文章
相關標籤/搜索