SpringBoot整合介面管理工具Swagger怎麼使用
一、Swagger簡介
Swagger 是一系列 RESTful API 的工具,透過 Swagger 可以獲得專案的⼀種互動式文檔,客戶端 SDK 的自動生成等功能。
Swagger 的目標是為REST APIs 定義一個標準的、與語⾔言無關的接口,使人和計算機在看不到源碼或者看不到文檔或者不能通過網絡流量檢測的情況下,能發現並理解各種服務的功能。當服務透過 Swagger 定義,消費者就能與遠端的服務互動透過少量的實現邏輯。
二、Springboot整合swagger
使用Spring Boot 整合Swagger 的理念是,使用註解來標記需要在API 文件中展示的訊息,Swagger 會根據專案中標記的註解來產生對應的API 文件。 Swagger 被號稱世界上最受歡迎的 API 工具,它提供了 API 管理的全套解決方案,API 文件管理需要考慮的因素基本上都包含,這裡將講解最常用的客製化內容。
1、新增swagger座標
Spring Boot 整合 Swagger 3 很簡單,需要引入依賴並做基礎配置即可。
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2、Swagger Helloword 實作
2.1、建立springboot專案
在啟動類別上加上@EnableOpenApi 註解啟用swagger api文件功能
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.oas.annotations.EnableOpenApi; @SpringBootApplication @EnableOpenApi //启动swagger api文档注解 public class SpringBootWithSwaggerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootWithSwaggerApplication.class, args); } }
# 2.2、寫一個介面
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 阿水 * @create 2023-04-11 9:54 */ @RestController() public class SwaggerController { @GetMapping ("hello") public String hello(String params) { return "hello swagger"+" param为:"+params; } }
2.3、存取位址
http://localhost:8080/swagger-ui/index.html
Swagger 透過註解表明此介面會產生文檔,包括介面名稱、請求方法、參數、返回訊息等1、
Api 註解和 ApiOperation 註解
@Api
语法: @Api(tags = "用户操作") 或 @Api(tags = {"用户操作","用户操作2"})
- ##@ApiOperation
语法: @ApiOperation(value = "", notes = "", response = ) 属性说明: value:方法说明标题 notes:方法详细描述 response:方法返回值类型
案例:使用@Api和@ApiOperation產生api文檔
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 阿水 * @create 2023-04-11 9:54 */ @RestController() @Api(tags = {"操作用户"}) public class SwaggerController { @GetMapping ("hello") @ApiOperation(value = "swagger请求",notes = "阿水的第一个swagger请求",response = String.class) public String hello(String params) { return "hello swagger"+" param为:"+params; } }
2、
ApiImplicitParams註解和 ApiImplicitParam
@ApiImplicitParams 註解和 #@ApiImplicitParam用於對方法中的非物件綁定到簡單類型時使用。)進行說明语法:
@ApiImplicitParams(value = {
@ApiImplicitParam(name="", value = "", type = "", required = true, paramType = "", defaultValue = "")
})
属性说明:
name:形参名称
value:形参的说明文字
type:形参类型
required:该参数是否必须 true|false
paramType: 用于描述参数是以何种方式传递到 Controller 的,它的值常见有: path, query, body 和 header
path 表示参数是『嵌在』路径中的,它和 @PathVariable 参数注解遥相呼应;
query 表示参数是以 query string 的形式传递到后台的(无论是 get 请求携带在 url 中,还是 post 请求携带在请求体中),它和 @RequestParam 参数注解遥相呼应;
header 表示参数是『藏在』请求头中传递到后台的,它和 @RequestHeader 参数注解遥相呼应的。
form 不常用.
defaultValue :参数默认值
案例:使用@ApiImplicitParams註解和@ApiImplicitParam 對方法參數進行說明
import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 阿水 * @create 2023-04-11 9:54 */ @RestController() @Api(tags = {"操作用户"}) public class SwaggerController { @GetMapping ("hello") @ApiOperation(value = "swagger请求",notes = "阿水的第一个swagger请求",response = String.class) @ApiImplicitParams(value ={ @ApiImplicitParam(name="param1", value = "参数名1", type = "String", required = true, paramType = "query", defaultValue = "阿水所想的默认值1" ), @ApiImplicitParam(name="param2", value = "参数名2", type = "String", required = true, paramType = "query", defaultValue = "阿水所想的默认值2" ) }) public String hello(String param1,String param2) { return "hello swagger"+" param1为:"+param1+"param2为"+param2; } }
3、ApiModel註解和ApiModelProperty
@ApiModel("用户类") @Data public class Users { @ApiModelProperty(value = "编码:主键") private Integer id; @ApiModelProperty(value = "用户名") private String username; @ApiModelProperty(value = "密码") private String password; }
/** * 添加用户 * * @param user * @return */ @PostMapping("/add") @ApiOperation(value = "添加用户",notes = "添加用户信息",response = ResponseResult.class) @ApiResponses({ @ApiResponse(code = 200, message = "添加成功", response = ResponseResult.class), @ApiResponse(code = 500, message = "添加失败", response = ResponseResult.class) }) public ResponseResult<Void> addUser(@RequestBody User user) { //获得生成的加密盐 user.setSalt(SaltUtils.getSalt()); int n = userService.addUser(user); if (n > 0) { return new ResponseResult<Void>(200, "添加成功"); } return new ResponseResult<Void>(500, "添加失败"); }
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; /** * Swagger配置类 */ @Configuration //项目启动时加载此类 public class SwaggerConfig { @Bean public Docket api(){ return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() // 此处自行修改为自己的 Controller 包路径。 .apis(RequestHandlerSelectors.basePackage("com.lps.controller")) .paths(PathSelectors.any()) .build(); } public ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("阿水的项目接口文挡") .description("阿水的 Project Swagger2 UserService Interface") //说明信息 .termsOfServiceUrl("http://localhost:8080/swagger-ui/index.html") //文档生成的主页地址 .version("1.0") //文档版本 .build(); } }
http.authorizeRequests().antMatchers( "/swagger-ui.html", "/swagger-ui/*", "/swagger-resources/**", "/v2/api-docs", "/v3/api-docs", "/webjars/**").permitAll() .anyRequest().authenticated();
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
以上是SpringBoot整合介面管理工具Swagger怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Jasypt介紹Jasypt是一個java庫,它允許開發員以最少的努力為他/她的專案添加基本的加密功能,並且不需要對加密工作原理有深入的了解用於單向和雙向加密的高安全性、基於標準的加密技術。加密密碼,文本,數字,二進位檔案...適合整合到基於Spring的應用程式中,開放API,用於任何JCE提供者...添加如下依賴:com.github.ulisesbocchiojasypt-spring-boot-starter2. 1.1Jasypt好處保護我們的系統安全,即使程式碼洩露,也可以保證資料來源的

隨著Web應用程式的不斷發展,API已經成為了現代Web應用開發的標準之一。然而,隨著API的數量和複雜度的增加,維護和文件化它們也變得越來越複雜。為了解決這個問題,Swagger應運而生。它是一種用於產生API文件的工具,可讓開發者更輕鬆地維護和文件化API,同時也提供了視覺化文件和其他各種功能。在本文中,我們將討論如何在PHP中使用Swagger生成A

Laravel開發:如何使用LaravelSwagger產生API文件?在開發網頁應用程式時,處理API文件往往是一項繁瑣但必不可少的任務。使用Swagger可以自動產生API文件並使其視覺化。在Laravel開發中,我們可以使用LaravelSwagger擴充包來輕鬆地產生SwaggerAPI文件。本文將指引您如何在L

一、Redis實現分散式鎖原理為什麼需要分散式鎖在聊分散式鎖之前,有必要先解釋一下,為什麼需要分散式鎖。與分散式鎖相對就的是單機鎖,我們在寫多執行緒程式時,避免同時操作一個共享變數產生資料問題,通常會使用一把鎖來互斥以保證共享變數的正確性,其使用範圍是在同一個進程中。如果換做是多個進程,需要同時操作一個共享資源,如何互斥?現在的業務應用通常是微服務架構,這也意味著一個應用會部署多個進程,多個進程如果需要修改MySQL中的同一行記錄,為了避免操作亂序導致髒數據,此時就需要引入分佈式鎖了。想要實現分

springboot讀取文件,打成jar包後訪問不到最新開發出現一種情況,springboot打成jar包後讀取不到文件,原因是打包之後,文件的虛擬路徑是無效的,只能通過流去讀取。文件在resources下publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

在Springboot+Mybatis-plus不使用SQL語句進行多表添加操作我所遇到的問題準備工作在測試環境下模擬思維分解一下:創建出一個帶有參數的BrandDTO對像模擬對後台傳遞參數我所遇到的問題我們都知道,在我們使用Mybatis-plus中進行多表操作是極其困難的,如果你不使用Mybatis-plus-join這一類的工具,你只能去配置對應的Mapper.xml文件,配置又臭又長的ResultMap,然後再寫對應的sql語句,這種方法雖然看上去很麻煩,但具有很高的靈活性,可以讓我們

1.自訂RedisTemplate1.1、RedisAPI預設序列化機制基於API的Redis快取實作是使用RedisTemplate範本進行資料快取操作的,這裡開啟RedisTemplate類,查看該類別的源碼資訊publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations,BeanClassLoaderAware{//聲明了value的各種序列化方式,初始值為空@NullableprivateRedisSe

Flask-RESTful和Swagger:Pythonweb應用程式中建立RESTfulAPI的最佳實務(第二部分)在上一篇文章中,我們探討如何使用Flask-RESTful和Swagger來建立RESTfulAPI的最佳實務。我們介紹了Flask-RESTful框架的基礎知識,並展示如何使用Swagger來建立RESTfulAPI的文件。本
