1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢(xún)
      選擇下列產(chǎn)品馬上在線(xiàn)溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      這篇文章主要介紹SpringBoot+Swagger-ui如何自動(dòng)生成API文檔,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

      創(chuàng)新互聯(lián)建站專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、磐石網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為磐石等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

      什么是Swagger

      Swagger是一個(gè)Restful風(fēng)格接口的文檔在線(xiàn)自動(dòng)生成和測(cè)試的框架

      官網(wǎng):http://swagger.io

      官方描述:The World's Most Popular Framework for APIs.

      先看一下 swagger-ui 生成的api 的效果吧

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      然后我們打開(kāi)查詢(xún)所有用戶(hù)的api 看到api 內(nèi)容

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      然后在服務(wù)器運(yùn)行的狀態(tài)下點(diǎn)擊 try it out 測(cè)試查詢(xún)功能

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      接著打開(kāi)新增的api 查看

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      好了這個(gè)就是自動(dòng)生成的api 效果。接下來(lái)我們就看怎么在我們的項(xiàng)目中使用swagger-ui 吧

      springboot 集成 swagger -ui

      1、添加依賴(lài)

      
            io.springfox
            springfox-swagger2
            2.2.2
          
          
            io.springfox
            springfox-swagger-ui
            2.2.2
      

      2、編寫(xiě)配置文件

      在application 同級(jí)目錄新建 Swagger2 文件

      package com.abel.example;
      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;
      /**
       * Created by yangyibo on 2018/9/7.
       */
      @Configuration
      @EnableSwagger2
      public class Swagger2 {
        /**
         * 創(chuàng)建API應(yīng)用
         * apiInfo() 增加API相關(guān)信息
         * 通過(guò)select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例,用來(lái)控制哪些接口暴露給Swagger來(lái)展現(xiàn),
         * 本例采用指定掃描的包路徑來(lái)定義指定要建立API的目錄。
         * @return
         */
        @Bean
        public Docket createRestApi() {
          return new Docket(DocumentationType.SWAGGER_2)
              .apiInfo(apiInfo())
              .select()
              .apis(RequestHandlerSelectors.basePackage("com.abel.example.controller"))
              .paths(PathSelectors.any())
              .build();
        }
        /**
         * 創(chuàng)建該API的基本信息(這些基本信息會(huì)展現(xiàn)在文檔頁(yè)面中)
         * 訪(fǎng)問(wèn)地址:http://項(xiàng)目實(shí)際地址/swagger-ui.html
         * @return
         */
        private ApiInfo apiInfo() {
          return new ApiInfoBuilder()
              .title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs")
              .description("更多請(qǐng)關(guān)注https://blog.csdn.net/u012373815")
              .termsOfServiceUrl("https://blog.csdn.net/u012373815")
              .contact("abel")
              .version("1.0")
              .build();
        }
      }

      3、在controller上添加注解,自動(dòng)生成API

      注意:

      package com.abel.example.controller;
      import javax.servlet.http.HttpServletRequest;
      import java.util.Map;
      import com.abel.example.bean.User;
      import io.swagger.annotations.*;
      import org.apache.log4j.Logger;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.http.HttpStatus;
      import org.springframework.http.ResponseEntity;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.*;
      import com.abel.example.service.UserService;
      import com.abel.example.util.CommonUtil;
      @Controller
      @RequestMapping(value = "/users")
      @Api(value = "用戶(hù)的增刪改查")
      public class UserController {
        @Autowired
        private UserService userService;
        /**
         * 查詢(xún)所有的用戶(hù)
         * api :localhost:8099/users
         * @return
         */
        @RequestMapping(method = RequestMethod.GET)
        @ResponseBody
        @ApiOperation(value = "獲取用戶(hù)列表,目前沒(méi)有分頁(yè)")
        public ResponseEntity findAll() {
          return new ResponseEntity<>(userService.listUsers(), HttpStatus.OK);
        }
        /**
         * 通過(guò)id 查找用戶(hù)
         * api :localhost:8099/users/1
         * @param id
         * @return
         */
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        @ResponseBody
        @ApiOperation(value = "通過(guò)id獲取用戶(hù)信息", notes="返回用戶(hù)信息")
        public ResponseEntity getUserById(@PathVariable Integer id) {
          return new ResponseEntity<>(userService.getUserById(Long.valueOf(id)), HttpStatus.OK);
        }
        /**
         * 通過(guò)spring data jpa 調(diào)用方法
         * api :localhost:8099/users/byname?username=xxx
         * 通過(guò)用戶(hù)名查找用戶(hù)
         * @param request
         * @return
         */
        @RequestMapping(value = "/byname", method = RequestMethod.GET)
        @ResponseBody
        @ApiImplicitParam(paramType = "query",name= "username" ,value = "用戶(hù)名",dataType = "string")
        @ApiOperation(value = "通過(guò)用戶(hù)名獲取用戶(hù)信息", notes="返回用戶(hù)信息")
        public ResponseEntity getUserByUserName(HttpServletRequest request) {
          Map map = CommonUtil.getParameterMap(request);
          String username = (String) map.get("username");
          return new ResponseEntity<>(userService.getUserByUserName(username), HttpStatus.OK);
        }
        /**
         * 通過(guò)spring data jpa 調(diào)用方法
         * api :localhost:8099/users/byUserNameContain?username=xxx
         * 通過(guò)用戶(hù)名模糊查詢(xún)
         * @param request
         * @return
         */
        @RequestMapping(value = "/byUserNameContain", method = RequestMethod.GET)
        @ResponseBody
        @ApiImplicitParam(paramType = "query",name= "username" ,value = "用戶(hù)名",dataType = "string")
        @ApiOperation(value = "通過(guò)用戶(hù)名模糊搜索用戶(hù)信息", notes="返回用戶(hù)信息")
        public ResponseEntity getUsers(HttpServletRequest request) {
          Map map = CommonUtil.getParameterMap(request);
          String username = (String) map.get("username");
          return new ResponseEntity<>(userService.getByUsernameContaining(username), HttpStatus.OK);
        }
        /**
         * 添加用戶(hù)啊
         * api :localhost:8099/users
         * @param user
         * @return
         */
        @RequestMapping(method = RequestMethod.POST)
        @ResponseBody
        @ApiModelProperty(value="user",notes = "用戶(hù)信息的json串")
        @ApiOperation(value = "新增用戶(hù)", notes="返回新增的用戶(hù)信息")
        public ResponseEntity saveUser(@RequestBody User user) {
          return new ResponseEntity<>(userService.saveUser(user), HttpStatus.OK);
        }
        /**
         * 修改用戶(hù)信息
         * api :localhost:8099/users
         * @param user
         * @return
         */
        @RequestMapping(method = RequestMethod.PUT)
        @ResponseBody
        @ApiModelProperty(value="user",notes = "修改后用戶(hù)信息的json串")
        @ApiOperation(value = "新增用戶(hù)", notes="返回新增的用戶(hù)信息")
        public ResponseEntity updateUser(@RequestBody User user) {
          return new ResponseEntity<>(userService.updateUser(user), HttpStatus.OK);
        }
        /**
         * 通過(guò)ID刪除用戶(hù)
         * api :localhost:8099/users/2
         * @param id
         * @return
         */
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        @ResponseBody
        @ApiOperation(value = "通過(guò)id刪除用戶(hù)信息", notes="返回刪除狀態(tài)1 成功 0 失敗")
        public ResponseEntity deleteUser(@PathVariable Integer id) {
          return new ResponseEntity<>(userService.removeUser(id.longValue()), HttpStatus.OK);
        }
      }

      注解含義:

      @Api:用在類(lèi)上,說(shuō)明該類(lèi)的作用。
      @ApiOperation:注解來(lái)給API增加方法說(shuō)明。
      @ApiImplicitParams : 用在方法上包含一組參數(shù)說(shuō)明。
      @ApiImplicitParam:用來(lái)注解來(lái)給方法入?yún)⒃黾诱f(shuō)明。
      @ApiResponses:用于表示一組響應(yīng)
      @ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息
         code:數(shù)字,例如400
         message:信息,例如"請(qǐng)求參數(shù)沒(méi)填好"
         response:拋出異常的類(lèi)  
      @ApiModel:描述一個(gè)Model的信息(一般用在請(qǐng)求參數(shù)無(wú)法使用@ApiImplicitParam注解進(jìn)行描述的時(shí)候)
      @ApiModelProperty:描述一個(gè)model的屬性

      注意:@ApiImplicitParam的參數(shù)說(shuō)明:

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      paramType會(huì)直接影響程序的運(yùn)行期,如果paramType與方法參數(shù)獲取使用的注解不一致,會(huì)直接影響到參數(shù)的接收。

      4、啟動(dòng)項(xiàng)目效果圖:

      服務(wù)器啟動(dòng)后訪(fǎng)問(wèn) http://localhost:8099/swagger-ui.html 效果如下

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      點(diǎn)擊查看效果

      SpringBoot+Swagger-ui如何自動(dòng)生成API文檔

      以上是“SpringBoot+Swagger-ui如何自動(dòng)生成API文檔”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


      文章題目:SpringBoot+Swagger-ui如何自動(dòng)生成API文檔
      新聞來(lái)源:http://ef60e0e.cn/article/jjcjeg.html 99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
          1. <ul id="0c1fb"></ul>

            <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
            <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

            玉山县| 巴中市| 赤城县| 广元市| 沾益县| 武城县| 南汇区| 咸阳市| 夏邑县| 平阳县| 临漳县| 武夷山市| 建昌县| 佛山市| 上蔡县| 淳化县| 朔州市| 荥阳市| 龙井市| 滨海县| 云龙县| 绥中县| 措美县| 塔城市| 桑日县| 定襄县| 浮山县| 镇原县| 托里县| 盐源县| 株洲县| 阳朔县| 安徽省| 台江县| 当阳市| 霍城县| 邵东县| 石泉县| 杭州市| 北流市| 忻州市|