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服务接口的实现在哪里。由于您正在控制服务实现的创建,因此现在可以将其包装在代理中以转换异常或进行日志记录。
TA贡献1850条经验 获得超11个赞
这可以通过扩展RemoteServiceServlet和在例如processCall(RPCRequest rpcRequest)或中进行异常处理来完成doUnexpectedFailure(Throwable 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);
}
添加回答
举报