【问题标题】:How does UriComponentsBuilder avoid URISyntaxException?UriComponentsBuilder 如何避免 URISyntaxException?
【发布时间】: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

【问题讨论】:

    标签: java spring uri


    【解决方案1】:

    啊,找到了,正在这里捕获并转换为运行时IllegalStateException 异常:

    至于动机,Spring 通常倾向于运行时异常:why spring handles only unchecked exceptions

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2010-12-07
      • 2013-02-12
      • 1970-01-01
      • 2011-05-15
      • 2014-05-30
      • 2022-01-26
      • 2014-10-15
      • 2013-07-16
      相关资源
      最近更新 更多