You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过占位符替换构建动态URL?求URIBuilder外的更直接方案

直接替换URL模板占位符的方案

你有如下URL模板:

String template = "https://{subdomain}.apache.com/t/information/{information_service}";

需要将{subdomain}和{information_service}替换为动态值(示例值如下):

String subdomain = "newHouse";
String information_service = "rooms";

期望生成的URL:

String URL = "https://newHouse.apache.com/t/information/rooms";

注:每次迭代和会话中的值都会变化,以上仅为示例。

你尝试过用URIBuilder实现,但希望找到更直接的占位符替换方法,以下是几种可行方案:


1. 原生String.replace()(最直接)

直接调用字符串替换方法,适合占位符数量少的场景:

String template = "https://{subdomain}.apache.com/t/information/{information_service}";
String subdomain = "newHouse";
String information_service = "rooms";

String url = template.replace("{subdomain}", subdomain)
                     .replace("{information_service}", information_service);
// 输出:https://newHouse.apache.com/t/information/rooms

优点:无需依赖第三方库,代码简洁;缺点:占位符较多时需要编写多个replace调用,容易遗漏。

2. 使用JDK的String.format()

将模板调整为%s占位符形式,适合参数顺序固定的场景:

// 调整模板为位置占位符格式
String template = "https://%s.apache.com/t/information/%s";
String subdomain = "newHouse";
String information_service = "rooms";

String url = String.format(template, subdomain, information_service);

优点:JDK原生支持,语法简洁;缺点:占位符为位置索引,模板可读性稍弱,参数顺序不能出错。

3. 使用Apache Commons Text的StringSubstitutor

如果存在大量命名占位符,推荐用这个专门处理命名变量替换的工具类:
首先引入Maven依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.10.0</version>
</dependency>

然后编写替换逻辑:

import org.apache.commons.text.StringSubstitutor;
import java.util.HashMap;
import java.util.Map;

public class UrlTemplateExample {
    public static void main(String[] args) {
        String template = "https://{subdomain}.apache.com/t/information/{information_service}";
        Map<String, String> values = new HashMap<>();
        values.put("subdomain", "newHouse");
        values.put("information_service", "rooms");

        StringSubstitutor substitutor = new StringSubstitutor(values);
        String url = substitutor.replace(template);
        // 输出目标URL
    }
}

优点:支持批量替换命名占位符,可读性强,适配复杂模板场景;缺点:需要引入第三方库。

4. 自定义工具方法(灵活可控)

如果不想依赖第三方库,又要处理大量占位符,可以自己封装工具方法:

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlUtils {
    private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{(\\w+)\\}");

    public static String replaceUrlTemplate(String template, Map<String, String> params) {
        Matcher matcher = PLACEHOLDER_PATTERN.matcher(template);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String key = matcher.group(1);
            String value = params.getOrDefault(key, "");
            matcher.appendReplacement(sb, Matcher.quoteReplacement(value));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    // 使用示例
    public static void main(String[] args) {
        String template = "https://{subdomain}.apache.com/t/information/{information_service}";
        Map<String, String> params = Map.of(
            "subdomain", "newHouse",
            "information_service", "rooms"
        );
        String url = replaceUrlTemplate(template, params);
    }
}

优点:完全自定义,无需依赖,可根据需求扩展逻辑;缺点:需要自行处理正则匹配与替换逻辑,初期有一定开发成本。


内容的提问来源于stack exchange,提问作者coding2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 03:25:14