在我的GWT应用中。我覆盖了RemoteServiceServlet,以在调用服务方法之前检查 session 是否有效。我正在尝试从服务器抛出RuntimeException(“expired session”),我希望客户端从asynccallback onFailure捕获此异常...

在客户中,我想:
异步回调:

@Override
    public void onFailure(Throwable caught) {
        final String message = caught.getMessage();

        if (!isNullOrEmptyString(message) && message.contains("expired session")) {
            com.google.gwt.user.client.Window.Location.reload();
        }

    }

但是,在客户端中,捕获的对象仍然是StatusCodeException,并且消息仍然是默认的“...服务器中的异常...”。如果是从服务器发送的 session 已过期消息,如何至少覆盖默认消息以进行比较?

谢谢

嗨,古塞尔,
这是我的代码:
->自定义RemoteServiceServlet。我正在尝试在调用每个方法之前对其进行“拦截”。我检查了 session 并抛出RuntimeException(如果它已经过期)。因此,基本上,引发异常的不是声明的方法,而是自定义的RemoteServiceServlet。它仍然转到客户端异步中的“onFailure”,但是Throwable对象仍然是“StatusCodeException”类型,而没有EXPIRED_SESSION_MSG消息。不知道如何进行这项工作。谢谢!
public class XRemoteServiceServlet extends RemoteServiceServlet {
    private final static String EXPIRED_SESSION_MSG = "ERROR: Application has expired session.";
    @Override
    protected void onAfterRequestDeserialized(RPCRequest rpcRequest) {
        HttpServletRequest httpServletRequest = this.getThreadLocalRequest();
        HttpSession session = httpServletRequest.getSession(false);
        if (session != null) {
            final String sessionIdFromRequestHeader = getSessionIdFromHeader();
            if (!isNullOrEmptyString(sessionIdFromRequestHeader)) {
                final String sessionId = session.getId();

                if (!sessionId.equals(sessionIdFromRequestHeader)) {
                    throw new RuntimeException(EXPIRED_SESSION_MSG);
                }
            }

最佳答案

如果您未在远程方法声明中声明它们,则gwt应用程序的服务器端抛出的所有RuntimeExceptions都将被包装为StatusCodeException

编辑:

之后,Thomas Broyer发表评论,我了解到,在远程方法声明中声明的所有异常(已检查或未检查)都将传播到gwt客户端。因此,您要做的只是声明您的远程方法,例如:

public void myRemoteMethod() throws RuntimeException;

09-10 13:44
查看更多