Search code examples
javatomcatgwtcorsgwt-rpc

GWT- How to make RPC call(use server side) of one project in some other project?


We have created our web project on GWT with java. Totally working fine. Now, we are moving towards making the version of website for mobile which will also be on gwt and will be having a different url for sure(like m.exam.com).

ISSUE:-

We want to use the server side of our web project as server side of the mobile website. So that only front-end have to be changes. As gone through some tutorials got that its possible that gwt rpc call made from android app.

But we want that it should be possible in gwt to gwt RPC call.

For android app they use Syncproxy where they change the baseurl and use it to call the gwt server side methods. It will be for sure the reusability of the code.

ANDROID EXAMPLE:-

SyncProxy.setBaseURL("http://testing.enggheads.in/enggheads/");

        GreetingServiceAsync greetService = SyncProxy.create(GreetingService.class);

So, How to achieve it for GWT project to use server side methods of other project throuhg this method?


EDIT NEW ISSUE :The import com.gdevelop cannot be resolved

I had gone through more of docs of gwt for the cross domain rpc calls like gwt to android app. I got the syncproxy 0.5 jar file, which is exactly the same as one for android. Having the same functions. I imported the jar file in the project and called for the functions of SyncProxy as follow.

public void onModuleLoad() {

    SyncProxy.setBaseURL("http://www.enggheads.in/CrossModuleCode/");
      final GreetingServiceAsync greetService = SyncProxy.create(GreetingService.class);

    final Button sendButton = new Button("Send");
    final TextBox nameField = new TextBox();
    nameField.setText("GWT User");
    final Label errorLabel = new Label();

    // We can add style names to widgets
    sendButton.addStyleName("sendButton");

    // Add the nameField and sendButton to the RootPanel
    // Use RootPanel.get() to get the entire body element
    RootPanel.get("nameFieldContainer").add(nameField);
    RootPanel.get("sendButtonContainer").add(sendButton);
    RootPanel.get("errorLabelContainer").add(errorLabel);

}

Now while compiling the project i am getting the following errors.error

EDIT 2:-

GWT-No 'Access-Control-Allow-Origin' header is present on the requested resource


Solution

  • try setting the service Entry Point in your child project
    ((ServiceDefTarget) greetService).setServiceEntryPoint("http://testing.enggheads.in/enggheads/");

    if your childproject is deployed withtin another domain you may need a CORS filter to allow cross site requests

    EDIT:
    How to set up a simple exampel:

    1. Create a "New Web Application project..." and select "generate project sample code" and name it "mobileapp"
    2. Create another "New Web Application project..." and select "generate project sample code" and name it "webapp"
    3. in the Mobileapp.java copy this code in the first line of onModuleLoad: ((ServiceDefTarget) greetingService).setServiceEntryPoint("http://127.0.0.1:8080/webapp/webapp/greet"); webapp/greet is the <url-pattern>/webapp/greet</url-pattern>from your web.xml. http://127.0.0.1:8080/webapp is the URL to your webapp
    4. Compile both applications and deploy them on the same tomcat instance. if you deploy them on different tomcats you have to use a CORS filter for your webapp and allow cross domain requests.
    5. open http://127.0.0.1:8080/mobileapp/ in your browser and press the Send button. It will use the Server method of your webapp
      Important: This only works if both GreetingService interfaces have the same methods... so always keep them in sync

    here you can download the two projects, as described above on your own risk: googledrive link