1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot 集成 Swagger-Bootstrap-UI,頁(yè)面更清爽!

        共 3555字,需瀏覽 8分鐘

         ·

        2020-12-08 11:04


        來(lái)源:sf.gg/a/1190000038170506

        • 一、添加Maven依賴(lài)
        • 二、添加配置類(lèi)
        • 三、啟動(dòng)項(xiàng)目
        • 四、常用注解
        • 五、其它
        • 六、可能遇到的問(wèn)題


        之前在創(chuàng)業(yè)公司待的時(shí)候,用過(guò)swagger,因?yàn)槲业谝惶靵?lái)這家公司工作,第一個(gè)任務(wù)就是做接口文檔自動(dòng)化。

        后來(lái)覺(jué)得它不太好用,在瀏覽技術(shù)網(wǎng)站的時(shí)候,偶然發(fā)現(xiàn)swagger-bootstrap-ui,于是便重構(gòu)了,把swagger-bootstrap-ui整合進(jìn)來(lái),后來(lái)發(fā)現(xiàn)不僅僅對(duì)我們后端有幫助,主要方便我們將接口進(jìn)行歸類(lèi),同樣對(duì)安卓小伙伴也有幫助,他們可以看這個(gè)接口文檔進(jìn)行聯(lián)調(diào)。

        當(dāng)初我使用swagger-boostrap-ui的時(shí)候,那個(gè)時(shí)候還是1.x版本,如今swagger-bootsrap-ui到2.x,同時(shí)也更改名字knife4j,適用場(chǎng)景從過(guò)去的單體到微服務(wù)。也算是見(jiàn)證咱們國(guó)人自己的開(kāi)源項(xiàng)目從小到大。

        該開(kāi)源項(xiàng)目GitHub地址:https://github.com/xiaoymin/S...

        該開(kāi)源項(xiàng)目中文文檔地址: https://doc.xiaominfo.com/

        一、添加Maven依賴(lài)

        <dependency>
        ????<groupId>io.springfoxgroupId>
        ????<artifactId>springfox-swagger2artifactId>
        ????<version>2.9.2version>
        dependency>
        <dependency>
        ????<groupId>com.github.xiaoymingroupId>
        ????<artifactId>swagger-bootstrap-uiartifactId>
        ????<version>1.9.6version>
        dependency>

        二、添加配置類(lèi)

        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("[email protected]")
        ????????????????.version("1.0")
        ????????????????.build();
        ????}
        }

        三、啟動(dòng)項(xiàng)目

        啟動(dòng)項(xiàng)目,不報(bào)錯(cuò),然后訪問(wèn)地址:http://ip:port/doc.html 即可

        效果圖,如下:

        測(cè)試接口,效果圖如下:

        調(diào)式相當(dāng)于用PostMan測(cè)試接口。

        四、常用注解

        和swagger一樣,swagger用的注解,swagger-bootstrap-ui仍能用。不過(guò)結(jié)合我的開(kāi)發(fā)經(jīng)驗(yàn)來(lái)看,最常用的也就兩個(gè),@Api和@ApiOperation。@Api的效果,如圖:

        @ApiOperation的效果,如圖:

        由此,我們很容易就看出來(lái),它們的含義是什么,一個(gè)是接口分類(lèi)說(shuō)明,一個(gè)是接口方法說(shuō)明。

        至于這里不用swagger的參數(shù)注解,主要原因是不想加太多的注解從而增加代碼的數(shù)量,造成太多冗余。

        例子中的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?=?{"用戶(hù)管理"},?description?=?"用戶(hù)管理")
        public?class?UserController?{
        ????@Autowired
        ?private?UsersService?usersService;
        ????@GetMapping("/list")
        ????@ApiOperation(value?=?"用戶(hù)列表")
        ????public?List?list()?{
        ????????return?usersService.list();
        ????}
        }

        五、可能遇到的問(wèn)題

        1.訪問(wèn)不到接口文檔界面白版

        一般是被攔截了(shiro或springsecurity機(jī)制)或者是配置錯(cuò)誤。

        2.訪問(wèn)接口文檔界面出來(lái)了,但掃描不到接口

        主要是配置類(lèi)的緣故,配置類(lèi)有個(gè)包掃描,必須配置為controller路徑。如圖所示:

        如果還有其它問(wèn)題,可以去官方文檔上找,官方文檔有一個(gè)常規(guī)問(wèn)題列表和解

        決方案,如圖所示:


        如果問(wèn)題非常奇葩的話,實(shí)在解決不了(在參考官方文檔說(shuō)明和搜索的前提下,仍解決不了,把問(wèn)題詳細(xì)描述和關(guān)鍵性代碼提到該開(kāi)源項(xiàng)目的issue上,向創(chuàng)造者求助)。


        后臺(tái)回復(fù)?學(xué)習(xí)資料?領(lǐng)取學(xué)習(xí)視頻


        如有收獲,點(diǎn)個(gè)在看,誠(chéng)摯感謝

        瀏覽 59
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            高清mv妈妈我想你看完泪目了 | 成人做爱视频免费观看 | 成 人 黄 色 免费 观 看 | 日韩视频网 | 苍井空一区二区三区四区 | 国产精品久久欠久久al换脸综合 | 日本在线高清 | 国产AV成人网站 | 和寡妇做爰过程A一片 | 插屁股网站 |