java - Play Framework 2.2 (java) WS 恢复 block 不捕获 HTTP 异常

标签 java exception playframework-2.2 recovery

(看似)调用已知良好 Web 服务的简单静态方法。

如果未找到匹配记录,服务将返回 HTTP 500/内部服务器错误,但调用的恢复 block 永远不会执行。

我是错过了显而易见的事情还是做了一些明显愚蠢的事情?

public static Promise<Property> ByPhone(String phone) {
  return WS.url("http://localhost:9000/data/property/" + phone)
           .get ()
           .map (
              new Function<WS.Response, Property>() {
                public Property apply (WS.Response response) {
                  System.out.println("got here: " + response.getStatusText());
                  Property property = null;
                  try {
                    property = _mapper.readValue(response.getBody(), Property.class);
                  } catch (Throwable t) {
                    t.printStackTrace();
                  }
                  return property;
                }
              }
            ).recover (
              new Function<Throwable, Property>() {
                public Property apply (Throwable t) {
                  System.out.println("never get here");
                  t.printStackTrace();
                  return null;
                }
              }
            );
}

最佳答案

recover 有助于从未捕获的异常中恢复。在这种情况下,HTTP 500 响应不会导致未捕获的 Throwable

据推测,500 意味着后面的 block 将抛出错误

property = _mapper.readValue(response.getBody(), Property.class);

但是,您已经将其包装在 try catch block 中。

关于java - Play Framework 2.2 (java) WS 恢复 block 不捕获 HTTP 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21011199/

相关文章:

mongodb - 处理 BSONDocument 中的可选值

java - Jena SDB 将文件读入模型

playframework - 如何在 Play 中的 Promise 中使用 Http.Context.current()?

java - 自定义 AlertDialog 更改/覆盖状态栏颜色,那么我该如何解决这个问题?

java - 带有 ADT 插件的 Eclipse 中没有 Android 项目选项

c# - 具有动态描述的异常助手工厂

PHP:像 Xdebug 一样打印捕获异常

java - Exoplayer 离线 - 下载消耗的数据量超过文件大小

java - Swagger 不工作

c# - AWS .net Core Lambda 项目中的全局异常处理程序?