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

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

我错过了明显或做点什么愚蠢的东西?

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响应不会导致未捕获的世代odicetagcode

可能是,500表示以下块将抛出错误

property = _mapper.readValue(response.getBody(), Property.class);
. 但是,您已经在尝试捕获块中包裹了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top