UrlParamParserUtils.java 1.27 KB
package com.diligrp.cashier.shared.util;

import com.diligrp.cashier.shared.exception.PlatformServiceException;

import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UrlParamParserUtils.java
 * @author dengwei
 * @version 1.0.0
 * @Description UrlParamParserUtils
 * @date 2025-12-25 15:34
 */
public class UrlParamParserUtils {
    public static Map<String, String> parseQueryParams(String url) {
        try {
            URI uri = new URI(url);
            String query = uri.getQuery();
            if (query == null || query.isEmpty()) {
                return new HashMap<>();
            }

            Map<String, String> params = new HashMap<>();
            String[] pairs = query.split("&");
            for (String pair : pairs) {
                String[] keyValue = pair.split("=", 2);
                String key = URLDecoder.decode(keyValue[0], StandardCharsets.UTF_8);
                String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], StandardCharsets.UTF_8) : "";
                params.put(key, value);
            }
            return params;
        } catch (Exception e) {
            throw new PlatformServiceException("Invalid URL");
        }
    }
}