我想为了处理传入的 GWT 调用,您使用一些 Spring MVC 控制器或一些 servlet。它可以有以下逻辑
try{
// decode payload from GWT call
com.google.gwt.user.server.rpc.RPC.decodeRequest(...)
// get spring bean responsible for actual business logic
Object bean = applicationContext.getBean(beanName);
// execute business logic and encode response
return RPC.invokeAndEncodeResponse(bean, ….)
} catch (com.google.gwt.user.server.rpc.UnexpectedException ex) {
// send unexpected exception to client
return RPC.encodeResponseForFailure(..., new MyCustomUnexpectedException(), …) ;
}
这个案例的解决方案
HttpServletRequest request = getRequest() ;
if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid()) {
return RPC.encodeResponseForFailure(..., new MyCustomSessionExpiredException(), …) ;
} else {
// first code snippet goes here
}
然后在客户端代码中捕获自定义会话过期异常。如果您不直接使用 RPC,请提供有关 GWT 和 Spring 之间的桥接实现的更多详细信息。
您还需要强制 GWT 编译器将 MyCustomSessionExpiredException 类型包含到序列化白名单中(以防止 GWT 安全策略停止向客户端传播异常的情况)。解决方案:在每个同步接口的每个方法签名中包含 MyCustomSessionExpiredException 类型:
@RemoteServiceRelativePath("productRpcService.rpc")
public interface ProductRpcService extends RemoteService {
List<Product> getAllProducts() throws ApplicationException;
void removeProduct(Product product) throws ApplicationException;
}
MyCustomSessionExpiredException extends ApplicationException
然后在客户端代码中显示弹窗:
public class ApplicationUncaughtExceptionHandler implements GWT.UncaughtExceptionHandler {
@Override
public void onUncaughtException(Throwable caught) {
if (caught instanceof MyCustomSessionExpiredException) {
Window.alert("Session expired");
}
}
}
// Inside of EntryPoint.onModuleLoad method
GWT.setUncaughtExceptionHandler(new ApplicationUncaughtExceptionHandler());