SpringBoot集成Swagger-Bootstrap-UI

以前在創業公司待的時候,用過swagger,由於我第一天來這家公司工做,第一個任務就是作接口文檔自動化。html

後來以爲它不太好用,在瀏覽技術網站的時候,偶然發現swagger-bootstrap-ui,因而便重構了,把swagger-bootstrap-ui整合進來,後來發現不單單對咱們後端有幫助,主要方便咱們將接口進行歸類,一樣對安卓小夥伴也有幫助,他們能夠看這個接口文檔進行聯調。當初我使用swagger-boostrap-ui的時候,那個時候仍是1.x版本,現在swagger-bootsrap-ui到2.x,同時也更更名字knife4j,適用場景從過去的單體到微服務。也算是見證我們國人本身的開源項目從小到大。java

該開源項目GitHub地址:
https://github.com/xiaoymin/S...git

該開源項目中文文檔地址:
https://doc.xiaominfo.com/github

1、添加Maven依賴

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

2、添加配置類

package com.blog.tutorial.config;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 15:46
 */@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
    @Bean
 public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.blog.tutorial.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui RESTful APIs")
                .description("swagger-bootstrap-ui")
                .termsOfServiceUrl("http://localhost:5050/")
                .contact("developer@mail.com")
                .version("1.0")
                .build();
    }
}

3、啓動項目

啓動項目,不報錯,而後訪問地址:
http://ip:port/doc.html 便可web

效果圖,以下:
image.pngspring

測試接口,效果圖以下:
image.pngbootstrap

調式至關於用PostMan測試接口。後端

4、經常使用註解

和swagger同樣,swagger用的註解,swagger-bootstrap-ui仍能用。
不過結合個人開發經驗來看,最經常使用的也就兩個,@Api和@ApiOperation。
@Api的效果,如圖:
image.pngapi

@ApiOperation的效果,如圖:
image.png
由此,咱們很容易就看出來,它們的含義是什麼,一個是接口分類說明,一個是接口方法說明。app

至於這裏不用swagger的參數註解,主要緣由是不想加太多的註解從而增長代碼的數量,形成太多冗餘。

例子中的Controller代碼:

package com.blog.tutorial.controller;
import com.blog.tutorial.entity.Users;
import com.blog.tutorial.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@RestController
@RequestMapping("/user")
@Api(tags = {"用戶管理"}, description = "用戶管理")
public class UserController {
    @Autowired
 private UsersService usersService;
    @GetMapping("/list")
    @ApiOperation(value = "用戶列表")
    public List<Users> list() {
        return usersService.list();
    }
}

5、其它

關於swagger整合系列,能夠參考以下:
MP實戰系列\(二\)之集成swagger

關於swagger-bootstrap整合系列,能夠參考:

MP實戰系列\(八\)之SpringBoot+Swagger2

springfox-swagger之swagger-bootstrap-ui

6、可能遇到的問題

1.訪問不到接口文檔界面白版

通常是被攔截了(shiro或springsecurity機制)或者是配置錯誤。

2.訪問接口文檔界面出來了,但掃描不到接口

主要是配置類的緣故,配置類有個包掃描,必須配置爲controller路徑。
如圖所示:
image.png

若是還有其它問題,能夠去官方文檔上找,官方文檔有一個常規問題列表和解決方案,如圖所示:
image.png

若是問題很是奇葩的話,實在解決不了(在參考官方文檔說明和搜索的前提下,仍解決不了,把問題詳細描述和關鍵性代碼提到該開源項目的issue上,向創造者求助)。

相關文章
相關標籤/搜索