From d738dcd024c392ff6f20fae6429634187660d668 Mon Sep 17 00:00:00 2001 From: hsiong Date: Sun, 28 Apr 2024 16:23:10 +0800 Subject: [PATCH] feat: close #46, close #47 --- pom.xml | 2 +- src/main/java/http/InitUtil.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 5057218..74ffc02 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ tech.ynfy ynfy-tool-httpconnect ${project.groupId}:${project.artifactId} - 1.1.9 + 1.1.10 A Http Util for Java https://github.com/ynfy-tech/ynfy-tool-httpconnect diff --git a/src/main/java/http/InitUtil.java b/src/main/java/http/InitUtil.java index 6a71123..178a803 100644 --- a/src/main/java/http/InitUtil.java +++ b/src/main/java/http/InitUtil.java @@ -174,7 +174,9 @@ protected String appendGetUrlParam(String url, T paramObj) { if (String.class.equals(paramObj.getClass())) { throw new IllegalArgumentException("Param can't be string! "); } - urlNameString.append("?"); + + urlNameString.append(url.contains("?") ? "&" : "?"); // 使用三元运算符来决定是追加 '&' 还是 '?' + Class c = paramObj.getClass(); Field[] fields = c.getDeclaredFields(); for (Field field : fields) { @@ -195,6 +197,15 @@ protected String appendGetUrlParam(String url, T paramObj) { if (filedValue instanceof Collection) { Collection collection = (Collection) filedValue; for (Object s : collection) { + + if (s == null) { + continue; + } + + if (!String.class.equals(s.getClass())) { + throw new IllegalArgumentException("List param should be string! "); + } + String filedValueStr = URLEncoder.encode((String) s, StandardCharsets.UTF_8); urlNameString.append(fieldName).append("=").append(filedValueStr).append("&"); }