|
@@ -1,15 +1,130 @@
|
|
|
package com.zhentao;
|
|
|
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.zhentao.controller.AppController;
|
|
|
+import com.zhentao.pojo.App;
|
|
|
+import com.zhentao.service.AppService;
|
|
|
+import com.zhentao.service.impl.AppServiceImpl;
|
|
|
+import com.zhentao.vo.Result;
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.boot.SpringApplication;
|
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import sun.net.www.http.HttpClient;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
@SpringBootApplication
|
|
|
@MapperScan("com.zhentao.dao")
|
|
|
-public class AppAppcalition {
|
|
|
+public class AppAppcalition{
|
|
|
+ private static final String BASE_URL = "http://localhost:8080/app";
|
|
|
+ private static final String APP_KEY = "your_app_key";
|
|
|
+ private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- SpringApplication.run(AppAppcalition.class,args);
|
|
|
+ SpringApplication.run(AppAppcalition.class, args);
|
|
|
+ // 发起POST请求的示例方法调用
|
|
|
+ postRequest();
|
|
|
+ // 发起GET请求的示例方法调用
|
|
|
+ getRequest();
|
|
|
+ }
|
|
|
+ private static void postRequest() {
|
|
|
+ App app = new App();
|
|
|
+ app.setId(5);
|
|
|
+ app.setGoodsid(2);
|
|
|
+ app.setReqtime(new Date());
|
|
|
+ app.setGoodsid(100);
|
|
|
+ app.setAmount(10);
|
|
|
+ app.setPrice(100.0);
|
|
|
+ app.setMobile("13800138000");
|
|
|
+ app.setNowdate(new Date());
|
|
|
+ String reqId = "testReqId";
|
|
|
+ String signature = generateSignature(String.valueOf(app.getId()), String.valueOf(app.getGoodsid()), reqId, String.valueOf(app.getReqtime()), APP_KEY);
|
|
|
+ String s = DigestUtil.md5Hex(signature);
|
|
|
+ app.setSign(s);
|
|
|
+
|
|
|
+ // 创建参数映射,存储所有请求参数
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("appId", app.getId());
|
|
|
+ params.put("sign", app.getSign());
|
|
|
+ params.put("reqTime", app.getReqtime());
|
|
|
+ params.put("goodsId", app.getGoodsid());
|
|
|
+ params.put("amount", app.getAmount());
|
|
|
+ params.put("price", app.getPrice());
|
|
|
+ params.put("mobile", app.getMobile());
|
|
|
+ params.put("nowDate", app.getNowdate());
|
|
|
+
|
|
|
+ // 发起POST请求,并获取响应
|
|
|
+ HttpResponse response = HttpRequest.post(BASE_URL + "/append")
|
|
|
+ .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());
|
|
|
}
|
|
|
+ private static void getRequest() {
|
|
|
+ App app = new App();
|
|
|
+ app.setId(5);
|
|
|
+ app.setReqtime(new Date());
|
|
|
+ app.setGoodsid(100);
|
|
|
+ app.setAmount(10);
|
|
|
+ app.setPrice(100.0);
|
|
|
+ app.setMobile("13800138000");
|
|
|
+ app.setNowdate(new Date());
|
|
|
+ String reqId = "testReqId";
|
|
|
+ String signature = generateSignature(String.valueOf(app.getId()), String.valueOf(app.getGoodsid()), reqId, String.valueOf(app.getReqtime()), APP_KEY);
|
|
|
+ String s = DigestUtil.md5Hex(signature);
|
|
|
+ app.setSign(s);
|
|
|
+
|
|
|
+ // 创建参数映射,存储所有请求参数
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("appId", app.getId());
|
|
|
+ params.put("sign", app.getSign());
|
|
|
+ params.put("reqTime", app.getReqtime());
|
|
|
+ params.put("goodsId", app.getGoodsid());
|
|
|
+ params.put("amount", app.getAmount());
|
|
|
+ params.put("price", app.getPrice());
|
|
|
+ params.put("mobile", app.getMobile());
|
|
|
+ params.put("nowDate", app.getNowdate());
|
|
|
+
|
|
|
+ // 将参数映射转换为查询字符串
|
|
|
+ String queryString = HttpUtil.toParams(params);
|
|
|
+ // 发起GET请求,并携带reqId作为头部信息
|
|
|
+ HttpResponse response = HttpRequest.get(BASE_URL + "/add?" + queryString)
|
|
|
+ .header("reqId", reqId)
|
|
|
+ .execute();
|
|
|
+
|
|
|
+ // 打印GET请求的响应状态
|
|
|
+ System.out.println("GET Response Status: " + response.getStatus());
|
|
|
+ // 打印GET请求的响应体内容
|
|
|
+ System.out.println("GET Response Body: " + response.body());
|
|
|
+ }
|
|
|
+// 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);
|
|
|
+// }
|
|
|
+public static String generateSignature(String appId, String goodsId, String reqId, String reqTime, String appKey) {
|
|
|
+ String input = appId + goodsId + reqId + reqTime + appKey;
|
|
|
+ return DigestUtil.md5Hex(input);
|
|
|
+}
|
|
|
}
|