Springboot集成Swagger2及常見配置(無坑版)

本文出自《愚公要移山》

收錄於《Springboot專題》中

這種整合的文章確實已經爛大街了,寫他一方面是補充我的springboot系列,另一方面確實還有一部分小夥伴沒用過。最重要的是,如果你忘記了這種整合的代碼。可以隨時查閱。

前言

現在的開發基本上都是前後端分離,前後端交互都是通過API文檔。有了API文檔大家各自開發,互不干擾。

1、傳統方式

傳統方式是文檔設計好之後,分別發給前端和後端人員。這樣有個缺點,接口信息一旦變化,文檔就需要重新發送給前後端人員。無法做到實時。所以浪費時間和精力。

2、swagger方式

我們的後臺應用集成了swagger之後,會自動暴露出我們的接口,而且這個接口形式還是通過restful風格發佈的。一旦後端的接口有變化,會立刻顯示出來,因此極大地提高了效率。

OK,基本上一句話就可以總結他的好處,那就是後端寫的api文檔可以通過swagger的形式實時的發佈出來,供前端人員查看。

3、其他方式

swagger的頁面說實話長得不好看,也有一些其他的方案,不是有很多bug,就是收費。目前swagger是使用的最多的。我目前也正在做這個樣的開源項目,基於swagger做出類似於其他方案的頁面,而且功能更加的強大。

一、代碼整合

前提條件是要新建一個springboot項目。這點就不演示了。

第一步:添加依賴

<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.9.2的版本是用的最多的,具體的可以直接去maven的官網去搜索,找一個使用量最多的版本即可。

第二步:配置

新建config包,創建SwaggerConfig類

@EnableSwagger2
@Configuration
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
             .apiInfo(apiInfo())
             .select()
             //爲當前包路徑,控制器類包
             .apis(RequestHandlerSelectors.basePackage("com.fdd.controller"))
            .paths(PathSelectors.any())
             .build();
    }
    //構建 api文檔的詳細信息函數
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            //頁面標題
           .title("XX平臺API接口文檔")
            //創建人
           .contact(new Contact("馮鼕鼕", "http://www.javachat.cc",  
                                "[email protected]"))
           //版本號
          .version("1.0")
           //描述
          .description("系統API描述")
          .build();
    }

這裏的配置也比較簡單。這裏有很多選項供我們去配置。如果我們的項目有多個組,只需要創建多個Docket即可。這時候掃描的包換成每個組的包路徑。

第三步:controller類中配置

新建一個controller包,然後創建HelloController類

@Api("Hello控制類")
@RestController 
public class HelloController {
    @GetMapping(value = "/user")
    public User getUser(){
        return new User("愚公要移山","123456");
    }
    @ApiOperation("可以指定參數的API")
    @PostMapping("/param")
    public String hello2(@ApiParam("用戶名") String name){
        return "hello" + name;
    }
}

這裏我們可以看出,使用註解就可以對這個類、方法、字段等等進行解釋說明。其他的字段還有很多,在使用的時候會有相應的提示,可以自己試一遍:

第四步:查看效果

訪問:http://127.0.0.1:8080/swagger-ui.html即可。

這裏就是最終的展示效果。OK,到這一步基本上就集成進來了。下面說一下可能會遇到的配置。

三、常見其他問題

1、Spring Security - 配置免認證訪問

有時候我們的Springboot集成了SpringSecurity,這時候如果訪問swagger的地址會自動跳轉到登錄頁面。這是因爲SpringSecurity對其進行了攔截。爲此我們只需要在我們的SpringSecurity配置一下進行放行即可。

現在配置一下,進行放行。在config包下新建一個SpringSecurityConfig類

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/v2/*").permitAll()
                .antMatchers("/csrf").permitAll()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
        ;
    }
}

此時就可以正常的訪問了。

2、爲swagger設置jwt

這種方式比較簡單,只需要一步即可。修改我們的swaggerConfig類即可。

@EnableSwagger2
@Configuration
public class Swagger2Config {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .securityContexts(Arrays.asList(securityContext()))
                .securitySchemes(Arrays.asList(apiKey()))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    //構建 api文檔的詳細信息函數
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //頁面標題
                .title("XX平臺API接口文檔")
                //創建人
                .contact(new Contact("馮鼕鼕", "http://www.javachat.cc",
                        "[email protected]"))
                //版本號
                .version("1.0")
                //描述
                .description("系統API描述")
                .build();
    }
    private ApiKey apiKey() {
        return new ApiKey("JWT", "Authorization", "header");
    }
    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(defaultAuth()).build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope 
            = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
    }

}

加了一些token驗證的代碼,比較簡單,關於JWT的東西,可以私下了解。這裏不贅述了。

3、隱藏Endpoint

有時候自己寫的controller,或者是controller裏面的接口方法不想讓前端人員看到,我們可以隱藏即可。

第一:隱藏整個controller

@ApiIgnore
@RestController
public class MyController {
    //方法
}

第二:隱藏某個接口方法1

@ApiIgnore
@ApiOperation(value = "描述信息")
@GetMapping("/getAuthor")
public String getAuthor() {
    return "愚公要移山";
}

第三:隱藏某個接口方法2

@ApiOperation(value = "描述信息", hidden = true)
@GetMapping("/get")
public LocalDate getDate() {
    return LocalDate.now();
}

OK,很多配置基本上就到這了。後續會繼續補充。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章