zhentao 1 kuukausi sitten
vanhempi
commit
bd95fe2cbc
1 muutettua tiedostoa jossa 58 lisäystä ja 0 poistoa
  1. 58 0
      src/main/java/com/zhentao/lianxi330/Lianxi330Application.java

+ 58 - 0
src/main/java/com/zhentao/lianxi330/Lianxi330Application.java

@@ -19,25 +19,47 @@ public class Lianxi330Application {
     private static final String BASE_URL = "http://localhost:8080/api";
     private static final String APP_KEY = "your_app_key";
     private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
+    /**
+     * 程序的入口点
+     * 这里使用SpringApplication.run方法来启动一个Spring应用
+     * @param args 命令行参数,允许从命令行向程序传递参数
+     */
     public static void main(String[] args) {
         SpringApplication.run(Lianxi330Application.class, args);
 
+        // 发起POST请求的示例方法调用
         postRequest();
+        // 发起GET请求的示例方法调用
         getRequest();
     }
+    /**
+     * 发起POST请求的方法
+     * 该方法构造一个POST请求,包含必要的请求参数和头部信息,并打印响应结果
+     */
     private static void postRequest() {
+        // 生成唯一的请求ID
         String reqId = UUID.randomUUID().toString();
+        // 应用ID
         String appId = "123456";
+        // 商品ID
         String goodsId = "G001";
+        // 请求时间戳
         long reqTime = System.currentTimeMillis();
+        // 当前日期时间
         LocalDateTime nowDate = LocalDateTime.now();
+        // 将当前日期时间格式化为字符串
         String nowDateStr = nowDate.format(FORMATTER);
+        // 商品数量
         int amount = 2;
+        // 商品价格
         double price = 99.9;
+        // 手机号码
         String mobile = "13800138000";
 
+        // 生成签名,用于验证请求的合法性
         String sign = generateSign(appId, goodsId, reqId, reqTime, APP_KEY);
 
+        // 创建参数映射,存储所有请求参数
         Map<String, Object> params = new HashMap<>();
         params.put("appId", appId);
         params.put("sign", sign);
@@ -48,29 +70,47 @@ public class Lianxi330Application {
         params.put("mobile", mobile);
         params.put("nowDate", nowDateStr);
 
+        // 发起POST请求,并获取响应
         HttpResponse response = HttpRequest.post(BASE_URL + "/post")
                 .header("reqId", reqId)
                 .header("Content-Type", "application/json")
                 .body(JSONUtil.toJsonStr(params))
                 .execute();
 
+        // 打印POST请求的响应状态码
         System.out.println("POST Response Status: " + response.getStatus());
+        // 打印POST请求的响应体
         System.out.println("POST Response Body: " + response.body());
     }
 
+    /**
+     * 发起GET请求并处理响应
+     * 该方法构造一个GET请求,包含必要的请求参数和头部信息,然后发送请求并打印响应状态和体内容
+     */
     private static void getRequest() {
+        // 生成唯一请求ID
         String reqId = UUID.randomUUID().toString();
+        // 应用ID
         String appId = "123456";
+        // 商品ID
         String goodsId = "G001";
+        // 请求时间戳
         long reqTime = System.currentTimeMillis();
+        // 当前日期时间
         LocalDateTime nowDate = LocalDateTime.now();
+        // 将当前日期时间格式化为字符串
         String nowDateStr = nowDate.format(FORMATTER);
+        // 商品数量
         int amount = 2;
+        // 商品价格
         double price = 99.9;
+        // 手机号码
         String mobile = "13800138000";
 
+        // 生成签名,用于验证请求的合法性
         String sign = generateSign(appId, goodsId, reqId, reqTime, APP_KEY);
 
+        // 创建参数映射,存储所有请求参数
         Map<String, Object> params = new HashMap<>();
         params.put("appId", appId);
         params.put("sign", sign);
@@ -81,17 +121,35 @@ public class Lianxi330Application {
         params.put("mobile", mobile);
         params.put("nowDate", nowDateStr);
 
+        // 将参数映射转换为查询字符串
         String queryString = HttpUtil.toParams(params);
+        // 发起GET请求,并携带reqId作为头部信息
         HttpResponse response = HttpRequest.get(BASE_URL + "/get?" + queryString)
                 .header("reqId", reqId)
                 .execute();
 
+        // 打印GET请求的响应状态
         System.out.println("GET Response Status: " + response.getStatus());
+        // 打印GET请求的响应体内容
         System.out.println("GET Response Body: " + response.body());
     }
 
+    /**
+     * 生成签名
+     * 该方法用于创建一个唯一的签名字符串,用于验证请求的合法性和完整性
+     * 它通过将应用ID、商品ID、请求ID、请求时间和应用密钥拼接后进行MD5加密来生成签名
+     *
+     * @param appId 应用ID,标识请求所属的应用
+     * @param goodsId 商品ID,标识请求涉及的商品
+     * @param reqId 请求ID,标识唯一的请求
+     * @param reqTime 请求时间,以毫秒为单位的时间戳
+     * @param appKey 应用密钥,用于加密的密钥
+     * @return 返回生成的签名字符串
+     */
     private static String generateSign(String appId, String goodsId, String reqId, long reqTime, String appKey) {
+        // 拼接源字符串,顺序为:应用ID、商品ID、请求ID、请求时间和应用密钥
         String source = appId + goodsId + reqId + reqTime + appKey;
+        // 使用MD5加密源字符串,生成签名
         return DigestUtil.md5Hex(source);
     }
 }