调用 http 服务时出现以下异常:
com.wm.app.b2b.server.ServiceException
Message: com.wm.net.NetException: [ISC.0064.9314] Authorization Required: Unauthorized
到目前为止,一切都很好。但我想以编程方式获取这些信息 - 而不是人类可读的翻译。我似乎没有机会获得状态码 401 或 100% 证明问题是 401 的东西。
编写试图获取“根本原因”(getCause ...)的包装器不起作用。没有其他“原因”……
我所拥有的只是可解析的字符串。任何想法?
更新
我找到了一种方法来完成它 - 使用不推荐使用的方法......:
...
try {
output =
Service.doInvoke( "my.package.authentication", "checkAuthentication", input );
} catch( final ServiceException sEx ) {
// if this is deprecated: how to we have to handle this in future?
final Throwable wrappedEx = sEx.getWrappedException();
// return early
if(null == wrappedEx || !NetException.class.isInstance(wrappedEx) ) {
throw sEx;
}
// process the net exception
final NetException nEx = (NetException)wrappedEx;
final String responseBody = convertStreamToString(nEx.getResponseInputStream());
// process the returned body wrapped by the net exception
final Gson gson = new Gson();
final ErrorData errorData = gson.fromJson(responseBody, ErrorData.class);
// check if the problem is an invalid token
tokenIsInvalid = errorData.code.equals(INVALID_TOKEN_EXCEPTION__CODE_STRING);
} catch( Exception e ) {
// wrap the exception in a service exception and throw it
throw new ServiceException(e);
}
...
更好的解决方案是简单地检查 HTTP-Status-Code - 但是如果 http-service 收到 401 将永远消失... :-|