GWT:无法反序列化响应

GWT:无法反序列化响应,gwt,gwt2,Gwt,Gwt2,我正在使用GWT(2.4),其中集成了Spring,如下所示。我无法从数据库(Hibernate)获取用户列表并用它填充DataGrid。当我调用greetingService.allUsers()方法时,我得到了错误(onFailure()): com.google.gwt.user.client.rpc.CompatibileMoteServiceException: 无法反序列化响应 有人帮忙吗?下面是一些代码。完整的工作项目是 不兼容EmoteServiceException的文档说明:

我正在使用GWT(2.4),其中集成了Spring,如下所示。我无法从数据库(Hibernate)获取用户列表并用它填充DataGrid。当我调用
greetingService.allUsers()
方法时,我得到了错误(onFailure()):

com.google.gwt.user.client.rpc.CompatibileMoteServiceException: 无法反序列化响应

有人帮忙吗?下面是一些代码。完整的工作项目是


不兼容EmoteServiceException的文档说明:

此异常可能由以下问题引起:
  • 无法通过{@link找到请求的{@link RemoteService} 在服务器上初始化#forName(String)}。
  • 请求的{@link {@link未实现RemoteService}接口 com.google.gwt.user.server.rpc.RemoteServiceServlet RemoteServiceServlet}实例,该实例配置为处理 请求。
  • 未定义或禁用请求的服务方法 由请求的{@link RemoteService}接口继承。

  • {@link RemoteService}方法中使用的类型之一 调用已添加或删除字段。
  • 客户端 代码从服务器接收类型,但它无法
    反序列化。


在您的例子中,最后一点是,您有一个无法序列化和反序列化的类型,这是一个错误,您的
User
类就是其中之一。您应该有一个传输对象,它实现了
com.google.gwt.user.client.rpc.IsSerializable
接口,用于通过网络传输用户对象。有关更多信息,请参阅:。GWT RPC方法参数和返回类型必须在客户端和服务器应用程序之间通过网络传输,因此它们必须是一致的

我会尝试几种方法

  • 在用户中,只需实现com.google.gwt.User.client.rpc.IsSerializable并添加一个空构造函数。我记得很久以前在某个地方读到,这是必要的,它在我的一个项目中解决了这样一个问题。 公共用户(){ }
  • 确保在gwt.xml文件中定义了包。

您没有做任何无法序列化的更复杂的事情,因此您应该可以接受。

我根据更新了
GwtRpcController
解决了我的问题。现在,反序列化工作良好,无需使用任何传输对象。工作
GwtRpcController
如下

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.server.rpc.RPC;
import com.google.gwt.user.server.rpc.RPCRequest;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class GwtRpcController extends RemoteServiceServlet implements
        Controller, ServletContextAware {

    private static final long serialVersionUID = 1L;

    private ServletContext servletContext;

    private RemoteService remoteService;

    private Class remoteServiceClass;

    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        super.doPost(request, response);
        return null;
    }

    @Override
    public String processCall(String payload) throws SerializationException {
        try {

            RPCRequest rpcRequest = RPC.decodeRequest(payload, this.remoteServiceClass, this);
            onAfterRequestDeserialized(rpcRequest);

            // delegate work to the spring injected service
            return RPC.invokeAndEncodeResponse(this.remoteService, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
        } catch (IncompatibleRemoteServiceException ex) {
            getServletContext().log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
            return RPC.encodeResponseForFailure(null, ex);
        }
    }

    @Override
    public ServletContext getServletContext() {
        return servletContext;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public void setRemoteService(RemoteService remoteService) {
        this.remoteService = remoteService;
        this.remoteServiceClass = this.remoteService.getClass();
    }

}
全部,, 花了很多时间在这上面,我有一个不同的解决方案。我使用的是一个非常简单的设置,我的POJO实际上只是一个成员和一个CTOR

我重新创建了一个新的GWT项目,并将我的东西重新添加到新项目中,将每个POM.xml依赖项重新添加到新项目中。我发现maven编译器设置得太高了。我是从另一个项目中抄来的,没有考虑过这个问题。。。GWT客户端几乎只有1.5,可能是1.6兼容。。。因此,这些设置需要设置为1.5,而不是1.7

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.2</version>
 <configuration>
  <source>1.7</source>                     <!-- change these to 1.5 -->
  <target>1.7</target>
 </configuration>
</plugin>

org.apache.maven.plugins
maven编译器插件
2.3.2
1.7
1.7

我在使用范围为“提供”的guava gwt时出现了这个错误。为了在运行时满足依赖关系,我添加了google集合。GWT客户端无法反序列化这些


解决方案:删除对google collections的依赖关系,并坚持使用guava。

要解决可序列化对象的问题,您可以尝试以下检查列表:

  • 验证该类是否具有默认构造函数(无参数)
  • 验证该类是否实现了可序列化或可序列化或 实现扩展可序列化或扩展类的接口 实现可序列化的
  • 验证该类是否在客户端中。*包或
  • 如果类不在client.*包中,请验证该类是否在中编译 您的GWT xml模块定义。默认情况下,存在。如果你的班级 在另一个包中,您必须将其添加到源。例如,如果 您的类位于域之下。*您应该将其作为添加到xml中。注意 该类不能属于服务器包!有关GWT的更多详细信息 第页:
  • 如果要包含来自另一个GWT项目的类,则必须 将继承添加到xml模块定义中。例如,如果您的 类Foo位于包com.dummy.domain中,您必须将其添加到 模块定义。详情如下:
  • 如果您包含另一个GWT项目中发布的类 jar验证jar是否也包含源代码,因为GWT 还要为传递给客户机的类重新编译Java源代码

  • Font:

    我在一个拥有成熟软件的团队中工作,该软件有一天因为这个错误而停止为我工作。其他队员都很好。我们尝试了很多方法来修复它。最后,我们重新安装了IntelliJ并修复了它。

    有时此错误可能是由于过时/损坏的客户端文件/缓存造成的(在这种情况下没有服务器端错误消息)。我刚刚在IntelliJ中运行了“重建模块”,它又恢复了正常。

    哦,可怕的无参数构造函数已经让我这么多次了。。。它可以是私人的。我有一个空白的承包商。您所说的“确保在gwt.xml文件中定义了包”是什么意思?在您的项目中,src的底部将有一个.gwt.xml文件。在底部你会看到我在说什么。以下是一个有用的链接:
    @Entity
    @Table(name = "users")
    public class User implements Serializable, IsSerializable {
    
        @Id
        private Long id;
    
        // only Strings and one Date
        private String login;
        private String password;
        private String firstname;
        private String lastname;
        private Date date;
    }
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.web.context.ServletContextAware;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
    import com.google.gwt.user.client.rpc.RemoteService;
    import com.google.gwt.user.client.rpc.SerializationException;
    import com.google.gwt.user.server.rpc.RPC;
    import com.google.gwt.user.server.rpc.RPCRequest;
    import com.google.gwt.user.server.rpc.RemoteServiceServlet;
    
    public class GwtRpcController extends RemoteServiceServlet implements
            Controller, ServletContextAware {
    
        private static final long serialVersionUID = 1L;
    
        private ServletContext servletContext;
    
        private RemoteService remoteService;
    
        private Class remoteServiceClass;
    
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            super.doPost(request, response);
            return null;
        }
    
        @Override
        public String processCall(String payload) throws SerializationException {
            try {
    
                RPCRequest rpcRequest = RPC.decodeRequest(payload, this.remoteServiceClass, this);
                onAfterRequestDeserialized(rpcRequest);
    
                // delegate work to the spring injected service
                return RPC.invokeAndEncodeResponse(this.remoteService, rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
            } catch (IncompatibleRemoteServiceException ex) {
                getServletContext().log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
                return RPC.encodeResponseForFailure(null, ex);
            }
        }
    
        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    
        @Override
        public void setServletContext(ServletContext servletContext) {
            this.servletContext = servletContext;
        }
    
        public void setRemoteService(RemoteService remoteService) {
            this.remoteService = remoteService;
            this.remoteServiceClass = this.remoteService.getClass();
        }
    
    }
    
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>2.3.2</version>
     <configuration>
      <source>1.7</source>                     <!-- change these to 1.5 -->
      <target>1.7</target>
     </configuration>
    </plugin>