兄弟們,真不騙你們,這個框架用起來是真的爽,簡直是服務器開發人員的福音!
- 集成該項目後,不用我們程序員再去處理api安全、加簽、驗籤、參數校驗、加解密、數據脱敏、異常處理、國際化、接口文檔、錯誤碼、緩存、分佈式鎖、應用、渠道管理等等功能。
- 而且為了幫助客户端開發的同學更簡單的接入後端api,它還提供了java版和Typescript版本的客户端工具包,而且也都是開源的。
- 不僅如此,而且像驗籤算法、加解密算法、請求參數、響應參數、錯誤碼等等功能還支持靈活定製化。
- 在嘗試使用了一下,發現這個框架使用起來非常方便,java版服務端和客户端只需要引入一個註解即可啓用所有功能
開源項目地址:
EasyApijava版服務端和客户端源碼:https://github.com/pddon/easy-spring-boot-apiEasyApitypescript版客户端源碼: https://github.com/pddon/easy-api-client
目測該項目剛開源沒幾天,如果覺得不錯的老鐵,可以動動你發財的手指,給它點點star哦!
使用示例
服務端代碼
我們採用它們官方的示例代碼編寫服務器端代碼
pom.xml文件引入maven依賴<!-- 基於springboot的api框架 --> <dependency> <groupId>com.pddon.framework</groupId> <artifactId>easy-spring-boot-api</artifactId> <version>1.0.0</version> </dependency>- 添加
springboot啓動類StartServerApplication.java@SpringBootApplication @EnableEasyApi public class StartServerApplication { public static void main(String[] args) { SpringApplication.run(StartServerApplication.class, args); } }
application.yml添加配置server: port: 8282 servlet: context-path: /test easyapi: #全局默認配置 #是否啓用EasyApi框架 #enable: true #是否開啓自動生成swagger接口文檔 #enableSwagger: true #默認語言,請求中或者會話中不存在語言參數時的默認取值 #locale: "zh_CN" #是否開啓多語言翻譯緩存 #enableLanguageCache: false #多語言緩存的最長時間,單位秒 #languageCacheSeconds: 180 #防重複提交碼過期時間 #noSubmitRepeatTimeoutSeconds: 120 #用户會話失效時間 #sessionExpireSeconds: 600 #當未找到該語言的翻譯時,是否使用默認翻譯內容 #alwaysUseDefaultLocale: false #是否強制對所有api響應添加響應殼 #forceAutoAddResponseWrapper: true #根包名集合,以逗號分隔 basePackages: "com.pddon.framework.demo.easyapi" #是否在控制枱打印所有配置信息 #printAllProperties: true error: #錯誤碼相關配置 #自定義系統錯誤碼值,格式【枚舉名 :數值】 systemErrorCodes: NOT_FOUND: 404 #業務異常定義,格式【錯誤碼多語言字典值 :錯誤碼指代的默認中文翻譯描述信息】 #業務錯誤碼默認起始值 businessCodeStart: 200001 #業務錯誤碼默認最大值 businessCodeEnd: 999999 #業務異常錯誤碼值 businessErrorCodes: USER_ACCOUNT_NOT_FOUND.desc: "用户賬號[{0}]未找到!" ACCOUNT_NOT_FOUND: code: 200006 desc: "賬户[{0}]未找到!" api: #業務接口api包名 swagger: #業務接口所在根包名,swagger會掃描這個包下的controller,生成您項目中中api的接口文檔 basePackage: "com.xxx.xxx" title: "業務API" description: "所有業務接口列表" termsOfServiceUrl: "www.xxx.com" contact: "xxx@xxx.xxx" version: "v1.0.0" #api請求系統參數配置 request: parameter: otherParams: #其他額外系統參數 - "appName" - "nickName" - "tagId" #rename: #重命名系統參數 #channelId: "buId" #api響應系統參數配置 #response: #field: #rename: #重命名系統響應參數 #code: "status" #渠道、應用信息配置 channels: default: #用於不區分渠道信息的應用 default: #默認的秘鑰信息 enable: true #用於對稱加解密、生成數字簽名、驗證數字簽名的秘鑰 secret: "EEFGcuwwEF76622A" keyPair: #用於非對稱加解密的秘鑰對 privateSecret: "" publicSecret: "" webApp: #某個應用下的秘鑰信息 enable: true #用於對稱加解密、生成數字簽名、驗證數字簽名的秘鑰 secret: "NKVNcuwwEF76622A" keyPair: #用於非對稱加解密的秘鑰對 privateSecret: "" publicSecret: "" android: default: enable: true #用於對稱加解密、生成數字簽名、驗證數字簽名的秘鑰 secret: "NK445uwwEF76622A" keyPair: #用於非對稱加解密的秘鑰對 privateSecret: "" publicSecret: "" ios: default: enable: true secret: "NKVNcuww89A6622A" keyPair: privateSecret: "" publicSecret: "" logging: level: org.springframework.web: INFO com.pddon.framework.easyapi: TRACE
- 編寫測試api,
TestController.java@RestController public class TestController { @PostMapping("prepay.do") @ApiOperation(value="應用下單", notes="應用向支付中心預下單") @RequiredSign(scope = SignScope.BOTH) public AppPreOrderResponse prepay(@RequestBody PrePayRequest request){ return service.prepay(request); } }
運行成功後,打開接口文檔地址
http://localhost:8282/test/doc.html也就是swagger-ui的國內升級版:knife4j,比swagger-ui的界面要好看多了。
java客户端代碼示例
pom.xml文件引入maven依賴<dependency> <groupId>com.pddon.framework</groupId> <artifactId>easy-spring-boot-api-client</artifactId> <version>1.0.0</version> </dependency>添加
springboot啓動類StartClientApplication.java@SpringBootApplication @EnableEasyApiClient public class StartClientApplication { public static void main(String[] args) { SpringApplication.run(StartServer.class, args); } }
application.yml添加配置easyapi: client: app: baseUrl: http://localhost:8282/test/ channelId: default appId: webApp secret: NKVNcuwwEF76622A locale: zh_CN privateSecret: publicSecret:接口調用使用示例
ClientApiTest.java@Component public class ClientApiTest { @Autowired private ApplicationConfig applicationConfig; private static ApiClient client; @PostConstruct public void init(){ client = ApiClient.newInstance(applicationConfig); } public DefaultResponseWrapper<AppPreOrderResponse> prepay(){ PrePayRequest request = PrePayRequest.builder() .appId(client.getConfig().getAppId()) .orderId("easyapi_client001") .description("just a test!") .currency(CurrencyType.CNY) .totalAmount(1) .originalAmount(10) .userId("U008956") .userNickname("小明") .build(); ApiInfo apiInfo = ApiInfo.builder() .apiName("/trade/prepay.do") .method(HttpMethod.POST) .needSign(true) .needSignResult(true) .build(); DefaultResponseWrapper<AppPreOrderResponse> response = client.executeRequest(apiInfo, request, AppPreOrderResponse.class, new HashMap<>()); log.info(JSONUtil.toJsonStr(response)); return response; } }結果
2024-03-14 20:14:24.284 INFO 4820 --- [ main] c.p.f.easyapi.client.ClientApiTest : {"code":0,"data":{"outTradeNo":"202403142011433578389649","orderId":"easyapi_client001","redirectUrl":"https://www.xxx.com/h5.html"},"sign":"A3731FA3BA2704BC51F4633DCC2C1BD4B232A41B","timestamp":1710418464218}
Typescript客户端調用示例
安裝依賴
npm i easy-api-client或者:
yarn add easy-api-client編寫測試代碼
import {ApiClient, newApiClientInstance, RequestType} from "easy-api-client"; let client: ApiClient = newApiClientInstance({ baseUrl: "http://localhost:8989/payment/", channelId: "local_test", appId: "pddon-payment-demo", secret: "NKVNcuwwEF3sc22A", locale: "zh_CN", apiSuffix: ".do" }); client.request({ apiName: "/trade/prepay", type: RequestType.post, needSignResult: true, needSign: true }, { appId: client.options.appId, orderId: "easy_api_client001", description: "just a test!", currency: "CNY", totalAmount: 1, originalAmount: 5, userId: "U008958", userNickname: "小明" }).then((response: any) => { console.log(response.data); }, (reason: any) => { console.log(reason); });調用結果
{ code: 0, data: { outTradeNo: '202403112208576967291582', orderId: 'easy_api_client001', redirectUrl: 'https://www.xxx.com/h5.html' }, sign: '3DB9F78499E8F31D3800FA3415009BE3D19EBA19', timestamp: 1710418049919 }
總結
以上就是easy-spring-boot-api框架的服務器、java客户端、typescript客户端代碼示例,有沒有感覺特別容易上手,本文僅做拋磚引玉,其他更多好用功能還等大家一起來體驗,喜歡的小夥伴不要忘了點star哦!