有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java GWT手动序列化服务器上的域对象

我的GWT应用程序加载时做的第一件事是通过RequestFactory从服务器请求当前登录的用户。这会阻塞,因为我需要用户的属性来知道如何继续。这只需要<;500毫秒,但这段时间应用被屏蔽了,真让我恼火。在生成jsp时,我已经在服务器上有了用户,那么为什么不将序列化用户添加到jsp中,并完全消除这个请求呢

我有两个问题阻止我这么做:

  • 我需要将用户转换为用户代理
  • 我需要以GWT易于反序列化的方式序列化UserProxy

我还没有想出一个好办法。这种逻辑似乎隐藏在ServiceLayerCorator中,没有一种简单的方法来隔离?我可能错了

第二种方法似乎更容易通过ProxySerializer实现,但当我在服务器上时,如何获得requestfactory?不能在服务器上调用GWT.create

我一直在研究AutoBeans,但这不处理上面的#1。我的UserProxy引用了我想要维护的其他EntityProxy的集合


共 (4) 个答案

  1. # 1 楼答案

    我在GWT上找到了答案。所有学分归Nisha Sowdri NM所有

    服务器端编码:

    DefaultProxyStore store = new DefaultProxyStore();
    ProxySerializer ser = requests.getSerializer(store);
    final String key = ser.serialize(userProxy);
    String message = key + ":" + store.encode();
    

    客户端解码:

    String[] parts = message.split(":", 2);
    ProxyStore store = new DefaultProxyStore(parts[1]);
    ProxySerializer ser = requests.getSerializer(store);
    UserProxy user = ser.deserialize(UserProxy.class, parts[0]);
    
  2. # 2 楼答案

    你不能给GWT打电话。在服务器上创建(或从任何真正的JVM创建),但在许多情况下,您可以调用为服务器使用而设计的JVM兼容方法。在本例中,请查看RequestFactorySource.create

    使用RequestFactory让服务器从自身读取数据并打印数据可能会有点混乱——下面是一个演示示例,演示了如何工作(使用gwt 2.4,主分支在2.3左右也有相同的功能)https://github.com/niloc132/tvguide-sample-parent/blob/gwt-2.4.0/tvguide-client/src/main/java/com/acme/gwt/server/TvViewerJsonBootstrap.java——与您想要的不太一样,但是,也可以使用同样的想法在代理存储中填充一个字符串,该字符串可以在客户端中读取(参见此处https://github.com/niloc132/tvguide-sample-parent/blob/gwt-2.4.0/tvguide-client/src/main/java/com/acme/gwt/client/TvGuide.java

    其基本思想是创建一个请求(包括id、调用和with()参数,以便代理生成器以一致的方式生成所有正确的部分),并将其传递到SimpleRequestProcessor实例,然后该实例将通过它通常会运行的服务器部分来运行它。(任何实体管理系统可能仍然应该缓存实体,以避免额外的查找,否则您需要对SRP内部没有的一些工作进行建模。)包装了ProxyStoreProxySerializer希望从服务器发送完整的RF消息,因此需要正确地进行相当多的消息记账

  3. # 3 楼答案

    如果你能够制作^{,你也可以使用AutoBeans来实现这个功能。它之所以有效,是因为代理是与getter/setter的接口:

    interface UserFactory implements AutoBeanFactory
    {
      AutoBean<UserProxy> user(UserProxy toWrap); // wrap existing instance in an AutoBean
    }
    

    然后在服务器上创建自动bean并序列化为json:

    UserFactory factory = AutoBeanFactorySource.create(UserFactory.class)
    AutoBean<UserProxy> userProxyBean = factory.user( existingUserPojo );
    
    // to convert AutoBean to JSON
    String json = AutoBeanCodex.encode(userProxyBean).getPayload();
    

    在客户端上,您可以直接使用AutoBeanCodex。解码将JSON反序列化回bean

  4. # 4 楼答案

    如果为代理创建AutoBeanFactory,则可以使用AutoBeans:

    • 将用户转换为用户代理: 创建一个服务器端RequestFactory并调用相同的普通请求。响应将包含UserProxy(但在服务器上)

    • 序列化用户代理:

      AutoBean<UserProxy> bean = AutoBeanUtils.getAutoBean(receivedUserProxy);

      String json = AutoBeanCodex.encode(bean).getPayload();

    • 要在客户端上反序列化UserProxy:

      AutoBean<UserProxy> bean = AutoBeanCodex.decode(userAutoBeanFactory, UserProxy.class, json);

    在服务器(tutorial)上创建进程中的RequestFactory:

    public static <T extends RequestFactory> T create( Class<T> requestFactoryClass ) {
      ServiceLayer serviceLayer = ServiceLayer.create();
      SimpleRequestProcessor processor = new SimpleRequestProcessor( serviceLayer );
      T factory = RequestFactorySource.create( requestFactoryClass );
      factory.initialize( new SimpleEventBus(), new InProcessRequestTransport(processor) );
      return factory;
    }