|
@@ -0,0 +1,79 @@
|
|
|
+package com.example.test0330;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.example.test0330.demos.web.pojo.RequestEntity;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class MainTest {
|
|
|
+
|
|
|
+ private static final String APP_KEY = "your_app_key";
|
|
|
+ private static final String BASE_URL = "http://localhost:7071/api";
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 模拟请求参数
|
|
|
+ RequestEntity request = new RequestEntity();
|
|
|
+ request.setAppId("123");
|
|
|
+ request.setGoodsId("456");
|
|
|
+ request.setReqTime(System.currentTimeMillis());
|
|
|
+ request.setAmount(1);
|
|
|
+ request.setPrice(100.0);
|
|
|
+ request.setMobile("13080440021");
|
|
|
+
|
|
|
+ // 将日期字符串转换为 Date 对象
|
|
|
+ String dateStr = "2023-04-27 12:55:22";
|
|
|
+ Date nowDate = DateUtil.parse(dateStr);
|
|
|
+ request.setNowDate(nowDate);
|
|
|
+
|
|
|
+ String reqId = "5555";
|
|
|
+
|
|
|
+ // 生成签名
|
|
|
+ String sign = generateSign(request.getAppId(), request.getGoodsId(), reqId, request.getReqTime(), APP_KEY);
|
|
|
+
|
|
|
+ request.setSign(sign);
|
|
|
+
|
|
|
+ // 调用 POST 接口
|
|
|
+ callPostApi(request, reqId);
|
|
|
+
|
|
|
+ // 调用 GET 接口
|
|
|
+ callGetApi(request, reqId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String generateSign(String appId, String goodsId, String reqId, Long reqTime, String appKey) {
|
|
|
+ String signStr = appId + goodsId + reqId + reqTime + appKey;
|
|
|
+ return DigestUtil.md5Hex(signStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void callPostApi(RequestEntity request, String reqId) {
|
|
|
+ JSONObject json = new JSONObject(request);
|
|
|
+ HttpResponse response = HttpRequest.post(BASE_URL + "/post")
|
|
|
+ .header("reqId", reqId)
|
|
|
+ .body(json.toString())
|
|
|
+ .execute();
|
|
|
+ System.out.println("POST 请求响应:" + response.body());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void callGetApi(RequestEntity request, String reqId) {
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("appId", request.getAppId());
|
|
|
+ paramMap.put("sign", request.getSign());
|
|
|
+ paramMap.put("reqTime", request.getReqTime());
|
|
|
+ paramMap.put("goodsId", request.getGoodsId());
|
|
|
+ paramMap.put("amount", request.getAmount());
|
|
|
+ paramMap.put("price", request.getPrice());
|
|
|
+ paramMap.put("mobile", request.getMobile());
|
|
|
+ paramMap.put("nowDate", request.getNowDate());
|
|
|
+
|
|
|
+ HttpResponse response = HttpRequest.get(BASE_URL + "/get")
|
|
|
+ .header("reqId", reqId)
|
|
|
+ .form(paramMap)
|
|
|
+ .execute();
|
|
|
+ System.out.println("GET 请求响应:" + response.body());
|
|
|
+ }
|
|
|
+}
|