[英]How to convert UriComponentsBuilder to java.net.URI
The documentation for UriComponentsBuilder
suggest that it is possible, but not straightforward to convert to a java.net.URI
. UriComponentsBuilder
的文档建议可以转换为java.net.URI
,但并非一帆风顺。
The following seems to work, but involves an intermediate serialization by toUriString()
. 以下内容似乎有效,但涉及到
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);
The constructor for java.net.URI
declares a throw: java.net.URI
的构造函数声明一个throw:
java.net.URISyntaxException;
java.net.URISyntaxException; must be caught or declared to be thrown
必须被抓住或宣布被抛出
Is there a nice way of doing this that doesn't require dealing with exceptions? 有没有一种不需要处理异常的好方法呢?
A really helpful answer might provide some insight as to why build
has different return types depending on the input type. 一个非常有用的答案可能会为您提供一些见解,说明为什么
build
会根据输入类型而具有不同的返回类型。
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.
新的UriComponentsBuilder类通过提供对URI各个方面的精细控制,包括构造,模板变量的扩展和编码,来帮助创建UriComponents实例。
The {type}
bind to queryParam in path. {type}
绑定到path中的queryParam。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.