如何将 UriComponentsBuilder 转换为 java.net.URI

How to convert UriComponentsBuilder to java.net.URI

UriComponentsBuilder 的文档表明可以转换为 java.net.URI

以下似乎可行,但涉及 toUriString() 的中间序列化。

UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(origin+base);
java.net.URI uri = null;
try {
    uri = new URI(uriBuilder.toUriString());
}
catch (Exception e) {}                                                              // TODO: insanity
String response = restTemplate.getForObject(uri,String.class);

java.net.URI 的构造函数声明了一个抛出:

java.net.URISyntaxException; must be caught or declared to be thrown

有没有不需要处理异常的好方法?

一个真正有用的答案可能会提供一些见解,说明为什么 build 具有不同的 return 类型,具体取决于输入类型。

A new UriComponentsBuilder class helps to create UriComponents instances by providing fine-grained control over all aspects of preparing a URI including construction, expansion from template variables, and encoding.

{type} 绑定到路径中的查询参数。

 URI uri = UriComponentsBuilder.newInstance().scheme("http").host("docuconv.claztec.net")
                .path("/cgi/{type}")
                .queryParam("path", file.getDownloadUrl())
                .queryParam("realname", file.getName())
                .queryParam("servicecode", file.getServiceCode())
                .queryParam("useragent", file.getUserAgent())
                .queryParam("charset", file.getCharset())
                .queryParam("format", file.getOption())
                .build().expand(file.getType())
                .encode()
                .toUri();

UriComponentsBuilder