添加接口

This commit is contained in:
yangjun 2025-05-28 19:14:06 +08:00
parent 5ffed27474
commit c4d0792c3d
3 changed files with 205 additions and 0 deletions

View File

@ -7,6 +7,18 @@
<groupId>org.jeecgframework.boot</groupId>
<version>3.7.0</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>
<artifactId>jeecg-module-demo</artifactId>

View File

@ -0,0 +1,82 @@
package org.jeecg.modules.demo.api.controller;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Map;
import java.util.TreeMap;
public class SignUtil {
/**
* 生成签名
* @param params 参数集合Map形式参数名为key参数值为value
* @param secret 密钥
* @return 签名结果小写MD5值
*/
public static String generateSign(Map<String, Object> params, String secret) {
// 1. 过滤无效参数并处理数组值
TreeMap<String, String> sortedParams = new TreeMap<>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
String key = entry.getKey();
if ("sign".equalsIgnoreCase(key)) continue; // 排除sign参数
Object value = entry.getValue();
if (value == null) continue; // 忽略空值
String processedValue = processValue(value);
if (processedValue.isEmpty()) continue; // 忽略处理后为空的参数
sortedParams.put(key, processedValue);
}
// 2. 拼接stringA
StringBuilder stringA = new StringBuilder();
for (Map.Entry<String, String> entry : sortedParams.entrySet()) {
if (stringA.length() > 0) {
stringA.append("&");
}
stringA.append(entry.getKey()).append("=").append(entry.getValue());
}
// 3. 拼接secret并生成签名
String stringSignTemp = stringA.toString() + secret;
return md5(stringSignTemp);
}
private static String processValue(Object value) {
if (value.getClass().isArray()) {
return convertArrayToString(value);
}
return value.toString();
}
private static String convertArrayToString(Object array) {
int length = Array.getLength(array);
if (length == 0) return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
Object element = Array.get(array, i);
sb.append(element != null ? element.toString() : "");
if (i < length - 1) {
sb.append(",");
}
}
return sb.toString();
}
private static String md5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xFF));
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException("MD5计算失败", e);
}
}
}

View File

@ -0,0 +1,111 @@
package org.jeecg.modules.demo.api.controller;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.blClsfzs.entity.BlClsfzs;
import org.jeecg.modules.demo.blClsfzs.service.IBlClsfzsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: jeecg-boot
* @Version: V1.0
*/
@RestController
@RequestMapping("/webApi")
@Slf4j
public class ZhApiController extends JeecgController<BlClsfzs, IBlClsfzsService> {
public static void main(String[] args) {
Map<String, Object> params = new HashMap<>();
params.put("_limit", "10");
params.put("_page", "2");
params.put("key", "XIAev9qlj-UOAEJ4");
params.put("noncestr", "123456");
String secret = "SE0l1BuC8SSCxO11pvTIvjCF_X6HP_ih";
String sign = SignUtil.generateSign(params, secret);
System.out.println(sign);
params.put("sign", sign);
String url = "http://hb.muin.cc/open/unusual/device/dtuStat";
postUrl(url,params);
}
public static String postUrl(String url,Map<String,Object> map){
Gson gson = new Gson();
String map2 = gson.toJson(map);
System.out.println(map2);
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为 POST
con.setRequestMethod("POST");
// 设置请求头
con.setRequestProperty("Content-Type", "application/json"); // 声明发送 JSON
con.setRequestProperty("Accept", "application/json"); // 声明期望接收 JSON
con.setDoOutput(true); // 允许写入请求体
// 写入 JSON 请求体
try (OutputStream os = con.getOutputStream()) {
byte[] input = map2.getBytes("utf-8");
os.write(input, 0, input.length);
}
// 获取响应码
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);
// 读取响应成功时
if (responseCode == HttpURLConnection.HTTP_OK) { // 200
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "UTF-8"))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println("Response: " + response.toString());
}
} else { // 错误时读取错误流
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getErrorStream(), "UTF-8"))) {
StringBuilder errorResponse = new StringBuilder();
String errorLine;
while ((errorLine = br.readLine()) != null) {
errorResponse.append(errorLine.trim());
}
System.out.println("Error Response: " + errorResponse.toString());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String mapToUrlParams(Map<String, Object> map) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> entry : map.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(entry.getKey()).append("=").append(entry.getValue());
}
return sb.toString();
}
}