如何做服裝微商城網(wǎng)站建設(shè)關(guān)鍵字廣告
一、postman介紹
1.1概述
工具下載
Postman(發(fā)送 http 請求的工具)
官網(wǎng)(下載速度比較慢):Download Postman | Get Started for Free
網(wǎng)盤下載:百度網(wǎng)盤 請輸入提取碼
1.2Http 請求格式
- 請求地址
- 請求方法
- 狀態(tài)碼
- 同源策略
- 請求頭
- 響應(yīng)頭
1.3接口類型
Post 接口(新增數(shù)據(jù))
@RequestMapping(method = RequestMethod.POST)
@PostMapping("/post")
如果你這樣寫,是 url 參數(shù),并且 url 參數(shù)可以為空
@PostMapping("/post") // http://localhost:9090/web/post?name=青哥哥&age=30
public Result post(Obj obj) {return Result.success(obj);
}
怎么請求 json 數(shù)據(jù)?
Put 接口(更新數(shù)據(jù))
@RequestMapping(method = RequestMethod.PUT)
@PutMapping("/put")
Delete 接口(刪除數(shù)據(jù))
@RequestMapping(method = RequestMethod.DELETE)
@DeleteMapping("/delete/{id}")
delete 可以傳 json 數(shù)據(jù)
我們批量刪除可以使用 delete 類型的接口
Get 接口
@RequestMapping()
或者
@GetMapping("/hello")
怎么定義路由
1.4Http 狀態(tài)碼
下述做常見的基本介紹,詳細請看有趣的小知識(一)HTTP請求響應(yīng)狀態(tài)碼:一份不可或缺的指南,從容面對任何請求挑戰(zhàn)!
- 200:成功
- 400:接口參數(shù)錯誤
- 404:接口路徑寫錯了或者參數(shù)寫錯了
- 405:接口請求類型不匹配
-
- 500:后臺錯誤
當(dāng)你的請求出現(xiàn)500 錯誤的時候,你應(yīng)該怎么辦?
第一時間,趕緊去看下后臺的控制臺
二、swagger
2.1文檔規(guī)范概述
OpenAPI規(guī)范(OpenAPI Specification簡稱OAS)是Linux基金會的一個項目,OpenAPI規(guī)范是用于描述API的行業(yè)標(biāo)準(zhǔn),它允許開發(fā)人員在不閱讀源代碼或文檔的情況下就能理解API的功能;通過JSON格式描述
?2.2API文檔神器Swagger介紹
Swagger是目前最受歡迎的基于OpenAPI規(guī)范的開源API構(gòu)建工具;
官網(wǎng):https:/swagger.io/
作用:在代碼中添加注解即可生成AP接口文檔;
<dependency><groupId>io.swagger.core.v3</groupId><artifactId>swagger-annotations</artifactId><version>2.2.20</version></dependency>
Swagger 是一套基于 OpenAPI 規(guī)范(OpenAPI Specification,OAS)構(gòu)建的開源工具,可以幫助我們設(shè)計、構(gòu)建、記錄以及使用 REST API。
OpenAPI規(guī)范是在2015年由OpenAPI Initiative捐贈給Linux基金會的。該規(guī)范創(chuàng)建了RESTful接口,可通過有效映射與之關(guān)聯(lián)的所有資源和操作來輕松開發(fā)和使用API??。
Swagger 主要包含了以下三個部分:
- Swagger Editor:基于瀏覽器的編輯器,我們可以使用它編寫我們 OpenAPI 規(guī)范。
- Swagger UI:它會將我們編寫的 OpenAPI 規(guī)范呈現(xiàn)為交互式的 API 文檔,后文我將使用瀏覽器來查看并且操作我們的 Rest API。
- Swagger Codegen:它可以通過為 OpenAPI(以前稱為 Swagger)規(guī)范定義的任何 API 生成服務(wù)器存根和客戶端 SDK 來簡化構(gòu)建過程。
2.3SpringFox
Springfox的Java庫套件旨在自動生成使用spring系列項目編寫的JSON API的機器和人類可讀規(guī)范。
Springfox的工作原理是在運行時檢查應(yīng)用程序,以基于Spring配置,類結(jié)構(gòu)和各種編譯時Java注釋來推斷API語義。
相關(guān)依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version>
</dependency>
相關(guān)注解
2.4使用
依賴
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.0.2</version></dependency>
常用注解
@Tag(name = "文件上傳下載" ,description = "文件上傳下載接口")public String File() {return "file_upload_download";}
@GetMapping("/download/{fileName}")@Operation(summary = "文件下載",description = "文件下載接口")public Result download(@PathVariable("fileName") String fileName, HttpServletResponse response) throws IOException {String filePath = ROOT_PATH + File.separator + fileName;if (!FileUtil.exist(filePath)) {return Result.error("文件不存在");}
// response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 預(yù)覽byte[] bytes = FileUtil.readBytes(filePath);ServletOutputStream outputStream = response.getOutputStream();outputStream.write(bytes); // 數(shù)組是一個字節(jié)數(shù)組,也就是文件的字節(jié)流數(shù)組outputStream.flush();outputStream.close();System.out.println("文件下載成功");return Result.success();}
@Schema(description = "返回結(jié)果")
public class Result {public static final String CODE_SUCCESS = "200";public static final String CODE_AUTH_ERROR = "401";public static final String CODE_SYS_ERROR = "500";@Schema(description = "狀態(tài)碼")private String code;@Schema(description = "消息")private String msg;@Schema(description = "數(shù)據(jù)")private Object data;
配置類
package com.yanyu.upload3.Config;import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration // 表示這是一個配置類
public class Swagger3Config1 {@Bean // 創(chuàng)建一個Beanpublic GroupedOpenApi FileApi() {// 創(chuàng)建一個GroupedOpenApi對象,設(shè)置其組名為"支付微服務(wù)模塊",并匹配所有以"/file/"開頭的路徑return GroupedOpenApi.builder().group("支付微服務(wù)模塊").pathsToMatch("/file/**").build();}@Bean // 創(chuàng)建一個Beanpublic GroupedOpenApi OtherApi() {// 創(chuàng)建一個GroupedOpenApi對象,設(shè)置其組名為"其它微服務(wù)模塊",并匹配所有以"/other/"開頭或等于"/others"的路徑return GroupedOpenApi.builder().group("其它微服務(wù)模塊").pathsToMatch("/other/**", "/others").build();}@Bean // 創(chuàng)建一個Beanpublic OpenAPI docsOpenApi() {// 創(chuàng)建一個OpenAPI對象,設(shè)置其標(biāo)題為"cloud2024",描述為"通用設(shè)計rest",版本為"v1.0"// 并設(shè)置其外部文檔的描述為"www.yanyu.com",URL為"https://yanyu.com/"return new OpenAPI().info(new Info().title("upload3").description("通用設(shè)計rest").version("v3.0")).externalDocs(new ExternalDocumentation().description("www.yanyu.com").url("\"https://yanyu.com/"));}
}