Friday, December 21, 2012

Re: using autobeans in a gwt-rpc call

package com.exemple.client.service;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.StatusCodeException;
import com.exemple.client.ClientAutoBeanUtils;
import com.exemple.domain.autobean.ServerInfoBean;

public class ServerInfoService {

public final static String PATH = "/serverInfo";

public void getServerInfo(final AsyncCallback<ServerInfoBean> callback){

RequestBuilder request = new RequestBuilder(RequestBuilder.POST, PATH);
request.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if(callback == null) return;
if(200 == response.getStatusCode()){
try{
callback.onSuccess(ClientAutoBeanUtils.decode(response.getText(), ServerInfoBean.class));
}catch(Exception caught){
callback.onFailure(caught);
}
}else{
callback.onFailure(new StatusCodeException(response.getStatusCode(), response.getText()));
}
}

@Override
public void onError(Request request, Throwable exception) {
if(callback != null){
callback.onFailure(exception);
}else if(GWT.getUncaughtExceptionHandler() != null){
GWT.getUncaughtExceptionHandler().onUncaughtException(exception);
}
}
});
try {
request.send();
} catch (RequestException exception) {
if(callback != null){
callback.onFailure(exception);
}else if(GWT.getUncaughtExceptionHandler() != null){
GWT.getUncaughtExceptionHandler().onUncaughtException(exception);
}
}

}

}
package com.exemple.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.exemple.client.service.ServerInfoService;
import com.exemple.domain.autobean.ServerInfoBean;
import com.exemple.server.ServerAutoBeanUtils;

@WebServlet(ServerInfoService.PATH)
public class ServerInfoServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.sendRedirect("/");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
AutoBean<ServerInfoBean> bean = ServerAutoBeanUtils.createServerInfoBean();
bean.as().setAvailable(true);
bean.as().setVersion("");
String responsePayload = ServerAutoBeanUtils.encode(bean);
// now you can write your response into resp...
}
}
gwt-rpc call needs concrete class type to do the serialization. So the generated autobean proxy won' t be serialized. 

If you do want to send your autobean object to server side, you need to use com.google.gwt.http.client.RequestBuilder (client side) to send the encoded string to server side, and create a servlet to handle this request.

I attached two files (used in our project), hope these can help you.


Le jeudi 20 décembre 2012 14:55:50 UTC+1, Cenk Oguz a écrit :
I have a small application where I am using autobeans extensively both on server and gwt side. I have an issue though with gwt-rpc calls, autobeans are apparently not serializable which causes gwt to fail with:

com.google.gwt.user.client.rpc.SerializationException: Type '$Proxy85' was not included in the set of types which can be serialized by this SerializationPolicy

Is there any obvious way that autobeans could be sent client->server side? Gwt-rpc is not good enough? Sending it over as a JSON string imposes some manual labor, seems overkill, would rather just go with POJOs in that case.

/Cenk

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/NMbom9Fr3WMJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment