欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2019年03月13日 15:32:51   作者:雙斜杠少年  
今天小編就為大家分享一篇關(guān)于SpringBoot+Swagger-ui自動(dòng)生成API文檔,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,現(xiàn)在的網(wǎng)站架構(gòu)基本都由原來的后端渲染,變成了:前端渲染、先后端分離的形態(tài),而且前端技術(shù)和后端技術(shù)在各自的道路上越走越遠(yuǎn)。

這樣后段開發(fā)好了api 之后就要提交api 文檔給前端的朋友。給前端的api 文檔各個(gè)公司有各個(gè)公司的要求,有的是word 有的是 md 文檔,或者是 postman 的一個(gè)連接。

好了廢話不多說說一下 swagger -ui 吧

什么是Swagger

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

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

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

先看一下 swagger-ui 生成的api 的效果吧(這是一個(gè)增刪改查的小李子)

然后我們打開查詢所有用戶的api 看到api 內(nèi)容

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

接著打開新增的api 查看

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

springboot 集成 swagger -ui

1、添加依賴

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

2、編寫配置文件

在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)信息
   * 通過select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例,用來控制哪些接口暴露給Swagger來展現(xiàn),
   * 本例采用指定掃描的包路徑來定義指定要建立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)在文檔頁面中)
   * 訪問地址: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 = "用戶的增刪改查")
public class UserController {
  @Autowired
  private UserService userService;
  /**
   * 查詢所有的用戶
   * api :localhost:8099/users
   * @return
   */
  @RequestMapping(method = RequestMethod.GET)
  @ResponseBody
  @ApiOperation(value = "獲取用戶列表,目前沒有分頁")
  public ResponseEntity<Object> findAll() {
    return new ResponseEntity<>(userService.listUsers(), HttpStatus.OK);
  }
  /**
   * 通過id 查找用戶
   * api :localhost:8099/users/1
   * @param id
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  @ResponseBody
  @ApiOperation(value = "通過id獲取用戶信息", notes="返回用戶信息")
  public ResponseEntity<Object> getUserById(@PathVariable Integer id) {
    return new ResponseEntity<>(userService.getUserById(Long.valueOf(id)), HttpStatus.OK);
  }
  /**
   * 通過spring data jpa 調(diào)用方法
   * api :localhost:8099/users/byname?username=xxx
   * 通過用戶名查找用戶
   * @param request
   * @return
   */
  @RequestMapping(value = "/byname", method = RequestMethod.GET)
  @ResponseBody
  @ApiImplicitParam(paramType = "query",name= "username" ,value = "用戶名",dataType = "string")
  @ApiOperation(value = "通過用戶名獲取用戶信息", notes="返回用戶信息")
  public ResponseEntity<Object> getUserByUserName(HttpServletRequest request) {
    Map<String, Object> map = CommonUtil.getParameterMap(request);
    String username = (String) map.get("username");
    return new ResponseEntity<>(userService.getUserByUserName(username), HttpStatus.OK);
  }
  /**
   * 通過spring data jpa 調(diào)用方法
   * api :localhost:8099/users/byUserNameContain?username=xxx
   * 通過用戶名模糊查詢
   * @param request
   * @return
   */
  @RequestMapping(value = "/byUserNameContain", method = RequestMethod.GET)
  @ResponseBody
  @ApiImplicitParam(paramType = "query",name= "username" ,value = "用戶名",dataType = "string")
  @ApiOperation(value = "通過用戶名模糊搜索用戶信息", notes="返回用戶信息")
  public ResponseEntity<Object> getUsers(HttpServletRequest request) {
    Map<String, Object> map = CommonUtil.getParameterMap(request);
    String username = (String) map.get("username");
    return new ResponseEntity<>(userService.getByUsernameContaining(username), HttpStatus.OK);
  }
  /**
   * 添加用戶啊
   * api :localhost:8099/users
   * @param user
   * @return
   */
  @RequestMapping(method = RequestMethod.POST)
  @ResponseBody
  @ApiModelProperty(value="user",notes = "用戶信息的json串")
  @ApiOperation(value = "新增用戶", notes="返回新增的用戶信息")
  public ResponseEntity<Object> saveUser(@RequestBody User user) {
    return new ResponseEntity<>(userService.saveUser(user), HttpStatus.OK);
  }
  /**
   * 修改用戶信息
   * api :localhost:8099/users
   * @param user
   * @return
   */
  @RequestMapping(method = RequestMethod.PUT)
  @ResponseBody
  @ApiModelProperty(value="user",notes = "修改后用戶信息的json串")
  @ApiOperation(value = "新增用戶", notes="返回新增的用戶信息")
  public ResponseEntity<Object> updateUser(@RequestBody User user) {
    return new ResponseEntity<>(userService.updateUser(user), HttpStatus.OK);
  }
  /**
   * 通過ID刪除用戶
   * api :localhost:8099/users/2
   * @param id
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  @ResponseBody
  @ApiOperation(value = "通過id刪除用戶信息", notes="返回刪除狀態(tài)1 成功 0 失敗")
  public ResponseEntity<Object> deleteUser(@PathVariable Integer id) {
    return new ResponseEntity<>(userService.removeUser(id.longValue()), HttpStatus.OK);
  }
}

注解含義:

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

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

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

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

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

點(diǎn)擊查看效果

本文項(xiàng)目全部代碼:https://github.com/527515025/springBoot/tree/master/springboot-swagger-ui

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • SpringMVC文件上傳中要解決的問題大匯總

    SpringMVC文件上傳中要解決的問題大匯總

    這篇文章主要介紹了SpringMVC文件上傳中要解決的問題,主要有中文文件名編碼問題,文件位置存儲(chǔ)問題以及文件名沖突問題等等,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • spring?jpa?審計(jì)功能自定義填充字段方式

    spring?jpa?審計(jì)功能自定義填充字段方式

    這篇文章主要介紹了spring?jpa審計(jì)功能自定義填充字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • springmvc如何使用map接收參數(shù)

    springmvc如何使用map接收參數(shù)

    這篇文章主要介紹了springmvc如何使用map接收參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java生成非對(duì)稱型加密公鑰和私鑰的方法

    Java生成非對(duì)稱型加密公鑰和私鑰的方法

    這篇文章主要介紹了Java生成非對(duì)稱型加密公鑰和私鑰的方法,涉及java非對(duì)稱加密的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • idea本地merge如何合并代碼

    idea本地merge如何合并代碼

    這篇文章主要介紹了idea本地merge如何合并代碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 基于HttpServletResponse 相關(guān)常用方法的應(yīng)用

    基于HttpServletResponse 相關(guān)常用方法的應(yīng)用

    本篇文章小編為大家介紹,基于HttpServletResponse 相關(guān)常用方法的應(yīng)用,需要的朋友參考下
    2013-04-04
  • Spring IOC 能降低耦合的問題分析及解決方法

    Spring IOC 能降低耦合的問題分析及解決方法

    這篇文章主要介紹了Spring IOC 為什么能降低耦合,依賴注入是調(diào)用者僅通過聲明某個(gè)組件就可以獲得組件的控制權(quán),而對(duì)該組件的依賴關(guān)系管理、查找、加載由外部完成,需要的朋友可以參考下
    2022-06-06
  • JAVA入門教學(xué)之快速搭建基本的springboot(從spring boot到spring cloud)

    JAVA入門教學(xué)之快速搭建基本的springboot(從spring boot到spring cloud)

    本文主要入門者介紹怎么搭建一個(gè)基礎(chǔ)的springboot環(huán)境,本文通過圖文并茂的形式給大家介紹從spring boot到spring cloud的完美搭建過程,適用java入門教學(xué),需要的朋友可以參考下
    2021-02-02
  • maven中springboot-maven-plugin的5種打包方式

    maven中springboot-maven-plugin的5種打包方式

    本文主要介紹了maven中springboot-maven-plugin的5種打包方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • MybatisX-Generator不生成domain文件夾的問題及解決

    MybatisX-Generator不生成domain文件夾的問題及解決

    在使用MybatisX-Generator生成數(shù)據(jù)庫表實(shí)體時(shí),如果發(fā)現(xiàn)沒有生成domain文件夾以及User.java文件,是因?yàn)镸ybatisX版本更新,最新版需要在options里額外勾選model才能生成domain,勾選model并點(diǎn)擊finish后,成功生成domain文件夾及User.java文件
    2025-01-01

最新評(píng)論