GWT RPC how to send any Object
I have a question concer开发者_JAVA技巧ning GWT RPC and self made classes.
So I create class for GWT like
public class B IsSerializable
{
public B(){}
private String b;
public B(String b)
{
this.b=b;
}
public String getB(){return this.b;}
}
And I want to sent the class to the GWT servlet. So I create method in servlet like a
public class TestService extends RemoteServiceServlet{
public String getServerReply(int a, B b)
{
return b.getB()+" and hello from server";
}
}
But I always get thrown exception in AsyncCallback method like
public void onFailure(Throwable caught) {
}
So I am confused and ask how to send B class to server?
I am looking forward to your advices
TestService(client-side):
public interface TestService extends RemoteService {
public B getString();
}
TestServiceAsync(client-side):
public interface TestServiceAsync {
public void getString(AsyncCallback<B> callback);
}
TestServiceImpl(server-side);
public class TestServiceImpl extends RemoteServiceServlet implements TestService {
public B getString() {
return new B("Some String from the server");
}
}
Making the call and doing somethinbg with the message:
TestServiceAsync service=(TestServiceAsync) GWT.create(TestService.class);
AsyncCallback<B> callback=new AsyncCallback<B>() {
public void onFailure(Throwable caught) {
Window.alert(caught.toString());
}
public void onSuccess(B result) {
Window.alert(result.getString());//Will show "Some String from the server"
}
}
service.getString(callback);
You need three files to make GWT-RPC work, 2 Interfaces(...Service and ...ServiceAsync) on the client and 1 class(...ServiceImpl) on the server.
Thanks :)
I know there should be 2 client side interfaces and so I did... And finally I succeeded to send B and to get a server reply The thing was
A) I forgot to implement the client service interface to my servlet (so the call was blocked)
B) I had to modify my B type into a client one
--Server side--
public String getServerReply(int a, com.mycompany.project.client.B b)
{ return b.getB()+" and hello from server"; }
And that worked fine :) But I haven't checked is the getB() is null but first step is done as I hope successfully :)
精彩评论