【发布时间】:2018-06-01 14:06:01
【问题描述】:
Spring的org.springframework.web.util.UriComponentsBuilder和相关的UriComponents.toUri()如何避免java.net.URI构造函数出现的检查异常URISyntaxException?
例如,使用new URI() 会导致必须捕获已检查的异常:
try {
uri = new URI(myUrl);
} catch (URISyntaxException e) {
// oops
}
对比使用 Spring 的构建器可以避免:
uri = UriComponentsBuilder.fromUriString(myUrl)
.build()
.toUri();
// no checked exception here
【问题讨论】: