Errai 编组不起作用

Errai marshalling doesn't work

我有一个基本的 POJO 对象,我想将其从服务器发送到客户端。 Here and here是如何使用Errai进行序列化。

User.java

package gwt.user.model;

import org.jboss.errai.common.client.api.annotations.MapsTo;
import org.jboss.errai.common.client.api.annotations.Portable;

@Portable
public class User{

  private String id;
  private String username;
  private String numberOfHits;

  public User(@MapsTo("id") String id, @MapsTo("username") String username, @MapsTo("numberOfHits") String numberOfHits) {
        this.id = id;
        this.username = username;
        this.numberOfHits = numberOfHits;
      }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getNumberOfHits() {
    return numberOfHits;
  }

  public void setNumberOfHits(String numberOfHits) {
    this.numberOfHits = numberOfHits;
  }

}

当我想发送这个用户对象时

MessageBuilder.createMessage().toSubject("ClientService").signalling().with("user", new User("8", "Johny", "32")).noErrorHandling().sendNowWith(this.dispatcher);  

我收到错误消息:没有可用于 type:gwt 的编组定义。user.model.User

java.lang.RuntimeException: no marshalling definition available for type:gwt.user.model.User
    at org.jboss.errai.marshalling.client.util.MarshallUtil.getMarshaller(MarshallUtil.java:135)
    at org.jboss.errai.marshalling.client.marshallers.ErraiProtocolEnvelopeMarshaller.marshall(ErraiProtocolEnvelopeMarshaller.java:100)
    at org.jboss.errai.marshalling.client.protocols.ErraiProtocol.encodePayload(ErraiProtocol.java:40)
    at org.jboss.errai.marshalling.server.protocol.ErraiProtocolServer.encodePayloadToByteArrayInputStream(ErraiProtocolServer.java:31)
    at org.jboss.errai.bus.server.io.BufferHelper.encodeAndWrite(BufferHelper.java:38)
    at org.jboss.errai.bus.server.ServerMessageBusImpl$RemoteMessageCallback.callback(ServerMessageBusImpl.java:664)
    at org.jboss.errai.bus.server.DeliveryPlan.deliver(DeliveryPlan.java:47)
    at org.jboss.errai.bus.server.ServerMessageBusImpl.sendGlobal(ServerMessageBusImpl.java:296)
    at org.jboss.errai.bus.server.ServerMessageBusImpl.send(ServerMessageBusImpl.java:354)
    at org.jboss.errai.bus.server.SimpleDispatcher.dispatch(SimpleDispatcher.java:60)
    at org.jboss.errai.bus.client.api.base.CommandMessage.sendNowWith(CommandMessage.java:349)
    at org.jboss.errai.bus.client.api.base.DefaultMessageBuilder.sendNowWith(DefaultMessageBuilder.java:95)
    at gwt.user.server.MessageController.sendMessage(MessageController.java:28)
    at gwt.user.server.MessageController.callback(MessageController.java:34)
    at org.jboss.errai.bus.server.DeliveryPlan.deliver(DeliveryPlan.java:47)
    at org.jboss.errai.bus.server.ServerMessageBusImpl.sendGlobal(ServerMessageBusImpl.java:296)
    at org.jboss.errai.bus.server.SimpleDispatcher.dispatchGlobal(SimpleDispatcher.java:46)
    at org.jboss.errai.bus.server.service.ErraiServiceImpl.store(ErraiServiceImpl.java:97)
    at org.jboss.errai.bus.server.service.ErraiServiceImpl.store(ErraiServiceImpl.java:114)
    at org.jboss.errai.bus.server.servlet.DefaultBlockingServlet.doPost(DefaultBlockingServlet.java:142)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
*** Message delivery failure ***
Bus: org.jboss.errai.bus.server.ServerMessageBusImpl@55d44a84
Message: ToSubject=ClientService, user=gwt.user.model.User@253c4d9f
errorMessage: Error calling remote service: ClientService
exception: java.lang.RuntimeException: no marshalling definition available for type:gwt.user.model.User
disconnect: false
org.jboss.errai.bus.client.api.base.MessageDeliveryFailure: could not deliver message: cannot have a conversation.  the incoming message has no session data associated with it.
    at org.jboss.errai.bus.client.api.base.CommandMessage.sendNowWith(CommandMessage.java:352)
    at org.jboss.errai.bus.client.api.base.DefaultMessageBuilder.sendNowWith(DefaultMessageBuilder.java:95)
    at gwt.user.server.MessageController.sendMessage(MessageController.java:28)
    at gwt.user.server.MessageController.callback(MessageController.java:34)
    at org.jboss.errai.bus.server.DeliveryPlan.deliver(DeliveryPlan.java:47)
    at org.jboss.errai.bus.server.ServerMessageBusImpl.sendGlobal(ServerMessageBusImpl.java:296)
    at org.jboss.errai.bus.server.SimpleDispatcher.dispatchGlobal(SimpleDispatcher.java:46)
    at org.jboss.errai.bus.server.service.ErraiServiceImpl.store(ErraiServiceImpl.java:97)
    at org.jboss.errai.bus.server.service.ErraiServiceImpl.store(ErraiServiceImpl.java:114)
    at org.jboss.errai.bus.server.servlet.DefaultBlockingServlet.doPost(DefaultBlockingServlet.java:142)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: cannot have a conversation.  the incoming message has no session data associated with it.
    at org.jboss.errai.bus.client.api.base.ConversationMessageWrapper.commit(ConversationMessageWrapper.java:294)
    at org.jboss.errai.bus.server.ServerMessageBusImpl.send(ServerMessageBusImpl.java:334)
    at org.jboss.errai.bus.client.api.base.ConversationMessageWrapper.sendNowWith(ConversationMessageWrapper.java:238)
    at org.jboss.errai.bus.client.api.base.DefaultMessageBuilder.sendNowWith(DefaultMessageBuilder.java:84)
    at org.jboss.errai.bus.client.util.ErrorHelper.sendClientError(ErrorHelper.java:143)
    at org.jboss.errai.bus.client.util.ErrorHelper.sendClientError(ErrorHelper.java:98)
    at org.jboss.errai.bus.client.util.ErrorHelper.handleMessageDeliveryFailure(ErrorHelper.java:234)
    at org.jboss.errai.bus.server.SimpleDispatcher.dispatch(SimpleDispatcher.java:67)
    at org.jboss.errai.bus.client.api.base.CommandMessage.sendNowWith(CommandMessage.java:349)
    ... 33 more
*** Message delivery failure ***
Bus: org.jboss.errai.bus.server.ServerMessageBusImpl@55d44a84
Message: ToSubject=MessageController
errorMessage: Error calling remote service: MessageController
exception: org.jboss.errai.bus.client.api.base.MessageDeliveryFailure: could not deliver message: cannot have a conversation.  the incoming message has no session data associated with it.

你需要一个空的构造函数才能工作

@Portable
public class User{
   public User(){}; // here
}

否则尝试切换到 Restlet GWT 版本,它也适用于 Errai。 参见:http://restlet.com/technical-resources/restlet-framework/guide/2.3/editions/gwt/overview