GWT: The response could not be deserialized

2019-04-28 05:19发布

I'm using GWT (2.4) with Spring integrated as in this article. I have problem with getting list of User from database (Hibernate) and populate DataGrid with it. When i call greetingService.allUsers() method, I'm getting error (onFailure()):

com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: The response could not be deserialized

Anybody helps with that? Below some pieces of code. Full working project is here.

  public void onModuleLoad() {
    // ...
    greetingService.allUsers(
        new AsyncCallback<List<User>>(){
            @Override
            public void onFailure(Throwable caught) {
                caught.printStackTrace();
            }
            @Override
            public void onSuccess(List<User> result) {
                GWT.log("SIZE: "+result.size());
                dataGrid.setRowData(result);
            }
        }
    );
    // ...
 }

GreetingServiceImpl

@Override
public List<User> allUsers() {
    return userDAO.findAll();
}

User

@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;
}

标签: gwt gwt2
8条回答
我只想做你的唯一
2楼-- · 2019-04-28 06:00

I solved my problem by updating GwtRpcController according to this. Now deserialization works good without using any transfer object. Working GwtRpcController below.

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();
    }

}
查看更多
干净又极端
3楼-- · 2019-04-28 06:03

All, Spent alot of time with this, and I have a different solution. I was using a very simple setup and my POJOs were really nothing more that members and a CTOR.

I recreated a new GWT project and added my stuff back into the new project, adding each POM.xml dependency back in. What I found was the maven compiler was set too high. I had copied this in from another project not thinking about it... GWT client side is pretty much only 1.5, maybe 1.6 compatible... so these settings need to be set to 1.5, not 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>
查看更多
登录 后发表回答