如何使用 URI 模板在 Java 中更改 URL 中的路径参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43653780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:38:32  来源:igfitidea点击:

how to use URI templates to change path parameters in a URL in java

javauriuritemplateurl-template

提问by Kasun Siyambalapitiya

I followed the tutorial available in herefor replacing path parameters with given values and ran the sample code which is given below

我按照此处提供的教程使用给定值替换路径参数并运行下面给出的示例代码

import org.glassfish.jersey.uri.UriTemplate;

import javax.ws.rs.core.UriBuilder;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class Demo {
    public static void main(String[] args) {
        String template = "http://example.com/name/{name}/age/{age}";
        UriTemplate uriTemplate = new UriTemplate(template);
        String uri = "http://example.com/name/Bob/age/47";
        Map<String, String> parameters = new HashMap<>();

        // Not this method returns false if the URI doesn't match, ignored
        // for the purposes of the this blog.
        uriTemplate.match(uri, parameters);
        System.out.println(parameters);
        parameters.put("name","Arnold");
        parameters.put("age","110");

        UriBuilder builder = UriBuilder.fromPath(template);
        URI output = builder.build(parameters);
        System.out.println(output.toASCIIString());


    }
}

but when I compile the code it gives me this error

但是当我编译代码时,它给了我这个错误

Exception in thread "main" java.lang.IllegalArgumentException: The template variable 'age' has no value

Exception in thread "main" java.lang.IllegalArgumentException: The template variable 'age' has no value

Please help me out to fix this, (may be my imports are causing the issue)

请帮我解决这个问题,(可能是我的导入导致了这个问题)

回答by Norb Braun

public static void main(String[] args) {
    String template = "http://example.com/name/{name}/age/{age}";
    UriTemplate uriTemplate = new UriTemplate(template);
    String uri = "http://example.com/name/Bob/age/47";
    Map<String, String> parameters = new HashMap<>();

    // Not this method returns false if the URI doesn't match, ignored
    // for the purposes of the this blog.
    uriTemplate.match(uri, parameters);
    System.out.println(parameters);
    parameters.put("name","Arnold");
    parameters.put("age","110");

    UriBuilder builder = UriBuilder.fromPath(template);
    // Use .buildFromMap()
    URI output = builder.buildFromMap(parameters);
    System.out.println(output.toASCIIString());

}

If you are using .buildfor filling the template, you have to provide the values one by one like .build("Arnold", "110"). In your case you want to use .buildFromMap()with your parametersmap.

如果.build用于填充模板,则必须一一提供值,例如.build("Arnold", "110"). 在您的情况下,您想.buildFromMap()parameters地图一起使用。