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

Spring Boot整合Swagger2的完整步驟詳解

 更新時間:2018年07月10日 14:55:15   作者:苦水潤喉  
這篇文章主要給大家介紹了關(guān)于Spring Boot整合Swagger2的完整步驟,文中通過示例代碼將整合的步驟一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

swagger,中文“拽”的意思。它是一個功能強(qiáng)大的api框架,它的集成非常簡單,不僅提供了在線文檔的查閱,
而且還提供了在線文檔的測試。另外swagger很容易構(gòu)建restful風(fēng)格的api。

一、Swagger概述

Swagger是一組圍繞OpenAPI規(guī)范構(gòu)建的開源工具,可幫助設(shè)計、構(gòu)建、記錄和使用REST API。

簡單說下,它的出現(xiàn)就是為了方便進(jìn)行測試后臺的restful形式的接口,實現(xiàn)動態(tài)的更新,當(dāng)我們在后臺的接口

修改了后,swagger可以實現(xiàn)自動的更新,而不需要認(rèn)為的維護(hù)這個接口進(jìn)行測試。

二、Swagger常用注解

swagger通過注解表明該接口會生成文檔,包括接口名、請求方法、參數(shù)、返回信息的等等。

  • @Api:修飾整個類,描述Controller的作用
  • @ApiOperation:描述一個類的一個方法,或者說一個接口
  • @ApiParam:單個參數(shù)描述
  • @ApiModel:用對象來接收參數(shù)
  • @ApiProperty:用對象接收參數(shù)時,描述對象的一個字段
  • @ApiResponse:HTTP響應(yīng)其中1個描述
  • @ApiResponses:HTTP響應(yīng)整體描述
  • @ApiIgnore:使用該注解忽略這個API
  • @ApiError :發(fā)生錯誤返回的信息
  • @ApiParamImplicitL:一個請求參數(shù)
  • @ApiParamsImplicit 多個請求參數(shù)

三、SpringBoot整合Swagger

3.1 添加依賴

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

3.2 添加SwaggerConfiguration

通過@Configuration注解,表明它是一個配置類,@EnableSwagger2開啟swagger2。

apiINfo()配置一些基本的信息。apis()指定掃描的包會生成文檔。

再通過createRestApi函數(shù)創(chuàng)建Docket的Bean之后,apiInfo()用來創(chuàng)建該Api的基本信息(這些基本信息會
展現(xiàn)在文檔頁面中)。select()函數(shù)返回一個ApiSelectorBuilder實例用來控制哪些接口暴露給Swagger來
展現(xiàn),本例采用指定掃描的包路徑來定義,Swagger會掃描該包下所有Controller定義的API,并產(chǎn)生文檔內(nèi)容
(除了被@ApiIgnore指定的請求)。

package com.lance.learn.springbootswagger.configuration;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author lance(ZYH)
 * @function Swagger啟動配置類
 * @date 2018-07-09 21:24
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

 /**
  * swagger2的配置文件,這里可以配置swagger2的一些基本的內(nèi)容,比如掃描的包等等
  * @return
  */
 @Bean
 public Docket createRestfulApi(){
  return new Docket(DocumentationType.SWAGGER_2)
    .pathMapping("/")
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.lance.learn.springbootswagger.controller")) //暴露接口地址的包路徑
    .paths(PathSelectors.any())
    .build();
 }

 /**
  * 構(gòu)建 api文檔的詳細(xì)信息函數(shù),注意這里的注解引用的是哪個
  * @return
  */
 private ApiInfo apiInfo(){
  return new ApiInfoBuilder()
    //頁面標(biāo)題
    .title("Spring Boot 測試使用 Swagger2 構(gòu)建RESTful API")
    //創(chuàng)建人
    .contact(new Contact("LanveToBigData", "http://www.cnblogs.com/zhangyinhua/", "917484312@qq.com"))
    //版本號
    .version("1.0")
    //描述
    .description("API 描述")
    .build();
 }
}

3.3 Controller文檔內(nèi)容

描述主要來源于函數(shù)等命名產(chǎn)生,對用戶并不友好,我們通常需要自己增加一些說明來豐富文檔內(nèi)容。

如下所示,我們通過@ApiOperation注解來給API增加說明、通過@ApiImplicitParams、@ApiImplicitParam
注解來給參數(shù)增加說明。

1)實例一

package com.lance.learn.springbootswagger.controller;

import com.lance.learn.springbootswagger.bean.Book;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * @author lance(ZYH)
 * @function
 * @date 2018-07-09 21:39
 */
@RestController
@RequestMapping(value = "/bookcurd")
public class BookController {
 Map<Long, Book> books = Collections.synchronizedMap(new HashMap<Long, Book>());

 @ApiOperation(value="獲取圖書列表", notes="獲取圖書列表")
 @RequestMapping(value={""}, method= RequestMethod.GET)
 public List<Book> getBook() {
  List<Book> book = new ArrayList<>(books.values());
  return book;
 }

 @ApiOperation(value="創(chuàng)建圖書", notes="創(chuàng)建圖書")
 @ApiImplicitParam(name = "book", value = "圖書詳細(xì)實體", required = true, dataType = "Book")
 @RequestMapping(value="", method=RequestMethod.POST)
 public String postBook(@RequestBody Book book) {
  books.put(book.getId(), book);
  return "success";
 }
 @ApiOperation(value="獲圖書細(xì)信息", notes="根據(jù)url的id來獲取詳細(xì)信息")
 @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long",paramType = "path")
 @RequestMapping(value="/{id}", method=RequestMethod.GET)
 public Book getBook(@PathVariable Long id) {
  return books.get(id);
 }

 @ApiOperation(value="更新信息", notes="根據(jù)url的id來指定更新圖書信息")
 @ApiImplicitParams({
   @ApiImplicitParam(name = "id", value = "圖書ID", required = true, dataType = "Long",paramType = "path"),
   @ApiImplicitParam(name = "book", value = "圖書實體book", required = true, dataType = "Book")
 })
 @RequestMapping(value="/{id}", method= RequestMethod.PUT)
 public String putUser(@PathVariable Long id, @RequestBody Book book) {
  Book book1 = books.get(id);
  book1.setName(book.getName());
  book1.setPrice(book.getPrice());
  books.put(id, book1);
  return "success";
 }
 @ApiOperation(value="刪除圖書", notes="根據(jù)url的id來指定刪除圖書")
 @ApiImplicitParam(name = "id", value = "圖書ID", required = true, dataType = "Long",paramType = "path")
 @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
 public String deleteUser(@PathVariable Long id) {
  books.remove(id);
  return "success";
 }

 @ApiIgnore//使用該注解忽略這個API
 @RequestMapping(value = "/hi", method = RequestMethod.GET)
 public String jsonTest() {
  return " hi you!";
 }
}

2)實例二

package com.lance.learn.springbootswagger.controller;

import com.lance.learn.springbootswagger.bean.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author lance(ZYH)
 * @function
 * @date 2018-07-09 22:00
 */

@RestController
@RequestMapping(value="/users")
public class UserDetailController {
  static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

  @ApiOperation(value="獲取用戶列表", notes="")
  @RequestMapping(value={""}, method= RequestMethod.GET)
  public List<User> getUserList() {
    List<User> r = new ArrayList<User>(users.values());
    return r;
  }

  @ApiOperation(value="創(chuàng)建用戶", notes="根據(jù)User對象創(chuàng)建用戶")
  @ApiImplicitParam(name = "user", value = "用戶詳細(xì)實體user", required = true, dataType = "User")
  @RequestMapping(value="", method=RequestMethod.POST)
  public String postUser(@RequestBody User user) {
    users.put(user.getId(), user);
    return "success";
  }

  @ApiOperation(value="獲取用戶詳細(xì)信息", notes="根據(jù)url的id來獲取用戶詳細(xì)信息")
  @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long")
  @RequestMapping(value="/{id}", method=RequestMethod.GET)
  public User getUser(@PathVariable Long id) {
    return users.get(id);
  }

  @ApiOperation(value="更新用戶詳細(xì)信息", notes="根據(jù)url的id來指定更新對象,并根據(jù)傳過來的user信息來更新用戶詳細(xì)信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long"),
      @ApiImplicitParam(name = "user", value = "用戶詳細(xì)實體user", required = true, dataType = "User")
  })
  @RequestMapping(value="/{id}", method=RequestMethod.PUT)
  public String putUser(@PathVariable Long id, @RequestBody User user) {
    User u = new User();
    users.put(id, u);
    return "success";
  }

  @ApiOperation(value="刪除用戶", notes="根據(jù)url的id來指定刪除對象")
  @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long")
  @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
  public String deleteUser(@PathVariable Long id) {
    users.remove(id);
    return "success";
  }
}

3.4 web界面查看

四、項目代碼地址

https://github.com/LanceToBigData/SpringBootLearning/tree/develop/SpringBoot-Swagger

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • SpringMVC 中HttpMessageConverter簡介和Http請求415 的問題

    SpringMVC 中HttpMessageConverter簡介和Http請求415 的問題

    本文介紹且記錄如何解決在SpringMVC 中遇到415 Unsupported Media Type 的問題,并且順便介紹Spring MVC的HTTP請求信息轉(zhuǎn)換器HttpMessageConverter
    2016-07-07
  • SpringBoot?攔截器返回false顯示跨域問題

    SpringBoot?攔截器返回false顯示跨域問題

    這篇文章主要介紹了SpringBoot?攔截器返回false顯示跨域問題,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-04-04
  • Mybatis-Plus中的selectByMap使用實例

    Mybatis-Plus中的selectByMap使用實例

    Mybatis-Plus來對數(shù)據(jù)庫進(jìn)行增刪改查時,將里面的函數(shù)試了個遍,接下來我就將使用selectByMap函數(shù)的簡單測試實例寫出來,方便沒有使用過的朋友們快速上手,感興趣的可以了解一下
    2021-11-11
  • JAVA實現(xiàn)長連接(含心跳檢測Demo)

    JAVA實現(xiàn)長連接(含心跳檢測Demo)

    這篇文章主要介紹了JAVA實現(xiàn)長連接(含心跳檢測Demo),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • SpringBoot使用validation做參數(shù)校驗的實現(xiàn)步驟

    SpringBoot使用validation做參數(shù)校驗的實現(xiàn)步驟

    這篇文章主要介紹了SpringBoot使用validation做參數(shù)校驗的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下
    2021-05-05
  • IDEA自定義Maven倉庫的實現(xiàn)

    IDEA自定義Maven倉庫的實現(xiàn)

    使用Maven進(jìn)行Java程序開發(fā)時,開發(fā)者能夠極大地提高開發(fā)效率,本文主要介紹了IDEA自定義Maven倉庫的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Java 實現(xiàn)貪吃蛇游戲的示例

    Java 實現(xiàn)貪吃蛇游戲的示例

    這篇文章主要介紹了Java 如何實現(xiàn)貪吃蛇游戲,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • Java的位圖和布隆過濾器深入詳細(xì)講解

    Java的位圖和布隆過濾器深入詳細(xì)講解

    這篇文章主要介紹了Java的位圖和布隆過濾器,在學(xué)習(xí)之前的數(shù)據(jù)結(jié)構(gòu)的時候,我們使用的數(shù)據(jù)量都不是很大,但是在生活中,我們常常面臨著要處理大量數(shù)據(jù)結(jié)果并得出最終結(jié)果,那么有沒有什么數(shù)據(jù)結(jié)構(gòu)可以幫助我們實現(xiàn)這樣的功能呢,想要繼續(xù)了解的朋友可以參考下
    2024-10-10
  • java 非對稱加密算法RSA實現(xiàn)詳解

    java 非對稱加密算法RSA實現(xiàn)詳解

    這篇文章主要介紹了java 非對稱加密算法RSA實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Java中初始化塊詳解及實例代碼

    Java中初始化塊詳解及實例代碼

    這篇文章主要介紹了Java中初始化塊詳解及實例代碼的相關(guān)資料,在Java中,有兩種初始化塊:靜態(tài)初始化塊和非靜態(tài)初始化塊,需要的朋友可以參考下
    2017-03-03

最新評論