播放框架2.2(Java)WS恢复块不会捕获HTTP异常
-
21-12-2019 - |
题
(似乎)简单的静态方法调用已知的好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);
.
但是,您已经在尝试捕获块中包裹了。 不隶属于 StackOverflow