2 回答

TA貢獻1839條經驗 獲得超15個贊
除了擴展服務之外RemoteServiceServlet,您還可以創建自己的servlet并委托給RemoteServiceServlet類似的東西:
public class GwtServiceServlet extends HttpServlet {
public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
Object delegate = ...;
RemoteServiceServlet remoteServiceServlet = new RemoteServiceServlet(delegate);
remoteServiceServlet.init(getServletConfig());
remoteServiceServlet.doPost(request, response);
}
}
delegate服務接口的實現在哪里。由于您正在控制服務實現的創建,因此現在可以將其包裝在代理中以轉換異?;蜻M行日志記錄。

TA貢獻1850條經驗 獲得超11個贊
這可以通過擴展RemoteServiceServlet和在例如processCall(RPCRequest rpcRequest)或中進行異常處理來完成doUnexpectedFailure(Throwable e)。
例如:
僅針對不屬于服務方法簽名的異?;蝈e誤,或由SecurityException,SerializationExceptions或RPC框架內的其他故障導致的異常或錯誤調用此方法。
意味著這里可以將任何NPE等映射到自定義異常。
protected void doUnexpectedFailure(Throwable e) {
try {
getThreadLocalResponse().reset();
} catch (IllegalStateException ex) {
/*
* If we can't reset the request, the only way to signal that something
* has gone wrong is to throw an exception from here. It should be the
* case that we call the user's implementation code before emitting data
* into the response, so the only time that gets tripped is if the object
* serialization code blows up.
*/
throw new RuntimeException("Unable to report failure", e);
}
ServletContext servletContext = getServletContext();
String code = magicallyGenerateErrorCode();
AwesomeException awesomeException = new AwesomeException("error", "Unexpected Error. Pls contact support", code);
RPCServletUtils.writeResponseForUnexpectedFailure(servletContext,
getThreadLocalResponse(), awesomeException);
}
添加回答
舉報