GWT manually serializes a domain object on a server

The first thing the GWT application does at startup is to request the current user from the server through RequestFactory. This blocks because I need user properties to know how to proceed. It only takes <500 ms, but it really annoys me that the application is blocked during this time. I already have a User on the server when jsp is generated, so why not just add the serialized user to jsp and completely exclude this request?

I have two problems: I cannot do this:

  • I need to convert User to UserProxy
  • I need to serialize UserProxy so that GWT can easily deserialize.

I have not found a good way to do # 1. Does this logic seem like ServiceLayerDecorator without an easy way to isolate? Maybe I'm wrong.

The second seems easier with ProxySerializer But how can I access the requestfactory when I'm on the server? You cannot call GWT.createon the server.

I studied AutoBeans , but this does not apply to # 1 above. My UserProxy has links to collections of other EntityProxy that I would like to support.

+5
source share
4 answers

You can use AutoBeans if you create an AutoBeanFactory for your proxies:

  • UserProxy: RequestFactory . UserProxy ( ).

  • UserProxy:

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

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

  • UserProxy :

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

in-process RequestFactory (tutorial):

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;
}
+5

AutoBeans , User implements UserProxy. , Proxies /:

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

autobean 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.decode JSON bean

+3

GWT.create ( 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).

, ( , invocations with(), ) SimpleRequestProcessor, , . ( , , , , SRP ). ProxySerializer, ProxyStore, RF- , .

+1

GWT Google Group. Nisha Sowdri NM.

Server Side Encoding:

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

Client Side Decoding:

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

All Articles