Spring Boot中使用Swagger2構建強大的RESTful(最新全,無坑)

1:說明

網上這類文章 太多, 一搜一大把 ,可是要不是知識太過於老舊,就是配置沒有說名清楚,你的項目按照他的配置卻不能正常運行:html

因此本文的目的: 配置swagger 2  那swagger 1 不說一下嗎,我以爲沒有必要了,確實須要以jar包方式構建 或者 維護老項目,那麼參考下面的鏈接git

https://github.com/swagger-api/swagger-ui/tree/2.x/dist  下載這個路徑內容,導入相關依賴便可,不建議使用github

 

2: swagger2 部署方式1

  2.1: 導入libweb

<!-- swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

 

  2.2 2: 建立配置類spring

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 掃描包路徑
                .apis(RequestHandlerSelectors.basePackage("com.dgw.controller"))
                // 掃描@APi 標記的Class
                //.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger2構建RESTful APIs")
                .description(" 項目 ")
                .contact(new Contact("dgw", "https://www.cnblogs.com/dgwblog/", "xxx@qq.com"))
                .version("1.0")
                .build();
    }
}

 

  2.3 基本上到這裏 網上那些教程讓你啓動 http://localhost:8080/swagger-ui.html# 訪問查看,而後介紹API就完事了 ? 他難道沒有用到 攔截器 Spring Boot 訪問映射 ? 你開發項目 就是一個hello world? 哈哈apache

  下面你必須配置資源映射 sping boot 2 在webmvcconfigurationsupport中配置api

/**
 * 支持webjars
 */
registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
/**
 * 支持swagger
 */
// 解決 SWAGGER 404報錯
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

 

  2.4 若是你的項目展現沒有使用到攔截器 那麼是能夠成功訪問的 ,可是最好知道須要配置攔截器mvc

registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/**")
        .excludePathPatterns("/user/login","/","/index")
           // swagger 排除規則
           .excludePathPatterns("/swagger-ui.html")
           .excludePathPatterns("/swagger-resources/**")
           .excludePathPatterns("/error")
           .excludePathPatterns("/webjars/**");

 

 

這個時候 訪問一下: 沒有問題:app

 

 

 

對了 若是你的項目用到 spring security 還須要排除如下配置ide

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
    }

}

 

 

 

3: swagger2 部署方式2 推薦

  導入lib'

<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.9.0.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 

application.xml配置

 

swagger.title=spring-boot-starter-swagger
swagger.description=Starter for swagger 2.x
swagger.version=1.4.0.RELEASE
swagger.license=Apache License, Version 2.0
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
swagger.contact.name=didi
swagger.contact.url=https://www.cnblogs.com/dgwblog/
swagger.contact.email=xxx@qq.com
# 掃描包路徑
swagger.base-package=com.dgw.controller
swagger.base-path=/**

 

啓動配置swagger 掃描

@SpringBootApplication 這個註解
@EnableSwagger2Doc
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

 

 

這裏若是出現了 建議看前面的 文章 ,本身考慮一下 爲何不能訪問.

 

這裏寫個測試

@Controller
@Api("接口說明")
public class HelloController {
    @ApiOperation(value = "hello方法 ",notes = "返回index")
    @GetMapping("/hello")
    public String hello(){
        return  "index";
    }
}

 

可以正常訪問:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章
相關標籤/搜索