Browse Source

修复Http请求参数序列化异常问题

woody 1 year ago
parent
commit
25fd3a3037

+ 24 - 19
framework-base/src/main/java/com/chelvc/framework/base/util/HttpUtils.java

@@ -128,7 +128,7 @@ public final class HttpUtils {
     }
 
     /**
-     * 获取Http请求体
+     * 获取Body请求参数
      *
      * @param request Http请求对象
      * @return 请求体
@@ -146,30 +146,19 @@ public final class HttpUtils {
     }
 
     /**
-     * 获取所有请求参数,包含表单参数及URL请求参数
+     * 获取表单请求参数
      *
      * @param request Http请求对象
      * @return 参数键/值映射表
      */
-    public static Map<String, String> getParameters(@NonNull HttpServletRequest request) {
+    public static Map<String, String[]> getParameters(@NonNull HttpServletRequest request) {
         // 获取表单参数
-        Map<String, String> parameters = Maps.newHashMap();
+        Map<String, String[]> parameters = Maps.newHashMap();
         Enumeration<String> enumeration = request.getParameterNames();
         while (enumeration.hasMoreElements()) {
             String name = enumeration.nextElement();
             if (StringUtils.nonEmpty(name)) {
-                parameters.put(name, request.getParameter(name));
-            }
-        }
-
-        // 获取资源地址参数
-        String query = request.getQueryString();
-        if (StringUtils.nonEmpty(query)) {
-            for (String parameter : query.split("&")) {
-                int separator = parameter.indexOf('=');
-                if (separator > 0) {
-                    parameters.put(parameter.substring(0, separator), parameter.substring(separator + 1));
-                }
+                parameters.put(name, request.getParameterValues(name));
             }
         }
         return parameters;
@@ -344,14 +333,30 @@ public final class HttpUtils {
      * @throws IOException I/O异常
      */
     public static String serialize(@NonNull HttpServletRequest request, boolean ordering) throws IOException {
-        // JSON请求方式
+        // Body参数
         String type = request.getContentType();
         if (StringUtils.nonEmpty(type) && type.contains(MediaType.APPLICATION_JSON_VALUE)) {
             return getBody(request);
         }
 
-        // 普通请求方式
-        return StringUtils.join(getParameters(request), "&", ordering);
+        // 表单参数
+        return StringUtils.join(getParameters(request), "&", (key, values) -> {
+            if (values == null || values.length == 0) {
+                return key + "=";
+            } else if (values.length == 1) {
+                return key + "=" + StringUtils.ifEmpty(values[0], StringUtils.EMPTY);
+            }
+            StringBuilder buffer = new StringBuilder();
+            for (String value : values) {
+                if (StringUtils.nonEmpty(value)) {
+                    if (buffer.length() > 0) {
+                        buffer.append("&");
+                    }
+                    buffer.append(key).append("=").append(value);
+                }
+            }
+            return buffer.length() == 0 ? (key + "=") : buffer.toString();
+        }, ordering);
     }
 
     /**