日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot学习笔记(16)----SpringBoot整合Swagger2

發布時間:2024/7/5 javascript 56 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot学习笔记(16)----SpringBoot整合Swagger2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Swagger 是一個規范和完整的框架,用于生成,描述,調用和可視化RESTful風格的web服務

  http://swagger.io

  Springfox的前身是swagger-springmvc,是一個開源的API doc框架,可以將我們的Controller接口的方法以文檔的形式展現,基于swagger,這樣就方便開發人員不用在開發完接口服務之后還需要手寫一份文檔給需要的人。

  使用方式如下

  引入依賴:

  pom.xml文件

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

  編寫配置類,并使用@EnableSwagger2開啟支持swagger2,配置類如下

  

package com.wangx.boot.util;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; 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; import static springfox.documentation.builders.PathSelectors.regex; @Configuration @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket accessToker() { return new Docket(DocumentationType.SWAGGER_2). groupName("api")//定一組 .select()//選擇那些路徑和接口api會生成document .apis(RequestHandlerSelectors.basePackage(""))//攔截的包 .paths(regex("/api/.*"))//攔截該路勁下的接口 .build() //創建
          .apiInfo(apiInfo()); //配置說明 } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("測試項目")//標題 .description("springboot整合swagger")//描述 .termsOfServiceUrl("http://www.baidu.com")// .contact(new Contact("wangx", "http://baidu.com","1028106567@qq.com"))//聯系人 .version("1.0")//版本 .build(); } }

  這樣就會攔截api路勁下的所有接口并生成document.

  訪問:http://localhost:8080/swagger-ui.html#/

  視圖如下:

  

  可以查看接口的相關信息,并且也可以不用通過postman就可以測試接口了。相當的便利的。

  同時swagger也提供了一些注解可以讓我們使用在類或者方法上

  Swagger的注解及作用。 

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

原文 SpringBoot學習筆記(16)----SpringBoot整合Swagger2

轉載于:https://www.cnblogs.com/xiaoshen666/p/10844089.html

總結

以上是生活随笔為你收集整理的SpringBoot学习笔记(16)----SpringBoot整合Swagger2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。