Problems passing class objects through GWT RPC
Asked Answered
B

8

20

I've run through the Google Web Toolkit StockWatcher Tutorial using Eclipse and the Google Plugin, and I'm attempting to make some basic changes to it so I can better understand the RPC framework.

I've modified the "getStocks" method on the StockServiceImpl server-side class so that it returns an array of Stock objects instead of String objects. The application compiles perfectly, but the Google Web Toolkit is returning the following error:

"No source code is available for type com.google.gwt.sample.stockwatcher.server.Stock; did you forget to inherit a required module?"

Google Web Toolkit Hosted Mode

It seems that the client-side classes can't find an implementation of the Stock object, even though the class has been imported. For reference, here is a screenshot of my package hierarchy:

Eclipse Package Hierarchy

I suspect that I'm missing something in web.xml, but I have no idea what it is. Can anyone point me in the right direction?

EDIT: Forgot to mention that the Stock class is persistable, so it needs to stay on the server-side.

Brunei answered 6/6, 2009 at 2:48 Comment(0)
B
23

After much trial and error, I managed to find a way to do this. It might not be the best way, but it works. Hopefully this post can save someone else a lot of time and effort.

These instructions assume that you have completed both the basic StockWatcher tutorial and the Google App Engine StockWatcher modifications.

Create a Client-Side Implementation of the Stock Class

There are a couple of things to keep in mind about GWT:

  1. Server-side classes can import client-side classes, but not vice-versa (usually).
  2. The client-side can't import any Google App Engine libraries (i.e. com.google.appengine.api.users.User)

Due to both items above, the client can never implement the Stock class that we created in com.google.gwt.sample.stockwatcher.server. Instead, we'll create a new client-side Stock class called StockClient.

StockClient.java:

package com.google.gwt.sample.stockwatcher.client;

import java.io.Serializable;
import java.util.Date;

public class StockClient implements Serializable {

  private Long id;
  private String symbol;
  private Date createDate;

  public StockClient() {
    this.createDate = new Date();
  }

  public StockClient(String symbol) {
    this.symbol = symbol;
    this.createDate = new Date();
  }

  public StockClient(Long id, String symbol, Date createDate) {
    this();
    this.id = id;
    this.symbol = symbol;
    this.createDate = createDate;
  }

  public Long getId() {
      return this.id;
  }

  public String getSymbol() {
      return this.symbol;
  }

  public Date getCreateDate() {
      return this.createDate;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public void setSymbol(String symbol) {
      this.symbol = symbol;
  }
}

Modify Client Classes to Use StockClient[] instead of String[]

Now we make some simple modifications to the client classes so that they know that the RPC call returns StockClient[] instead of String[].

StockService.java:

package com.google.gwt.sample.stockwatcher.client;

import com.google.gwt.sample.stockwatcher.client.NotLoggedInException;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("stock")
public interface StockService extends RemoteService {
  public Long addStock(String symbol) throws NotLoggedInException;
  public void removeStock(String symbol) throws NotLoggedInException;
  public StockClient[] getStocks() throws NotLoggedInException;
}

StockServiceAsync.java:

package com.google.gwt.sample.stockwatcher.client;

import com.google.gwt.sample.stockwatcher.client.StockClient;
import com.google.gwt.user.client.rpc.AsyncCallback;

public interface StockServiceAsync {
  public void addStock(String symbol, AsyncCallback<Long> async);
  public void removeStock(String symbol, AsyncCallback<Void> async);
  public void getStocks(AsyncCallback<StockClient[]> async);
}

StockWatcher.java:

Add one import:

import com.google.gwt.sample.stockwatcher.client.StockClient;

All other code stays the same, except addStock, loadStocks, and displayStocks:

private void loadStocks() {
    stockService = GWT.create(StockService.class);
    stockService.getStocks(new AsyncCallback<String[]>() {
        public void onFailure(Throwable error) {
            handleError(error);
        }

        public void onSuccess(String[] symbols) {
            displayStocks(symbols);
        }
    });
}

private void displayStocks(String[] symbols) {
    for (String symbol : symbols) {
        displayStock(symbol);
    }
}

private void addStock() {
    final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
    newSymbolTextBox.setFocus(true);

    // Stock code must be between 1 and 10 chars that are numbers, letters,
    // or dots.
    if (!symbol.matches("^[0-9a-zA-Z\\.]{1,10}$")) {
        Window.alert("'" + symbol + "' is not a valid symbol.");
        newSymbolTextBox.selectAll();
        return;
    }

    newSymbolTextBox.setText("");

    // Don't add the stock if it's already in the table.
    if (stocks.contains(symbol))
        return;

    addStock(new StockClient(symbol));
}

private void addStock(final StockClient stock) {
    stockService.addStock(stock.getSymbol(), new AsyncCallback<Long>() {
        public void onFailure(Throwable error) {
            handleError(error);
        }

        public void onSuccess(Long id) {
            stock.setId(id);
            displayStock(stock.getSymbol());
        }
    });
}

Modify the StockServiceImpl Class to Return StockClient[]

Finally, we modify the getStocks method of the StockServiceImpl class so that it translates the server-side Stock classes into client-side StockClient classes before returning the array.

StockServiceImpl.java

import com.google.gwt.sample.stockwatcher.client.StockClient;

We need to change the addStock method slightly so that the generated ID is returned:

public Long addStock(String symbol) throws NotLoggedInException {
  Stock stock = new Stock(getUser(), symbol);
  checkLoggedIn();
  PersistenceManager pm = getPersistenceManager();
  try {
    pm.makePersistent(stock);
  } finally {
    pm.close();
  }
  return stock.getId();
}

All other methods stay the same, except getStocks:

public StockClient[] getStocks() throws NotLoggedInException {
  checkLoggedIn();
  PersistenceManager pm = getPersistenceManager();
  List<StockClient> stockclients = new ArrayList<StockClient>();
  try {
    Query q = pm.newQuery(Stock.class, "user == u");
    q.declareParameters("com.google.appengine.api.users.User u");
    q.setOrdering("createDate");
    List<Stock> stocks = (List<Stock>) q.execute(getUser());
    for (Stock stock : stocks)
    {
       stockclients.add(new StockClient(stock.getId(), stock.getSymbol(), stock.getCreateDate()));
    }
  } finally {
    pm.close();
  }
  return (StockClient[]) stockclients.toArray(new StockClient[0]);
}

Summary

The code above works perfectly for me when deployed to Google App Engine, but triggers an error in Google Web Toolkit Hosted Mode:

SEVERE: [1244408678890000] javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.google.gwt.sample.stockwatcher.client.StockClient[] com.google.gwt.sample.stockwatcher.client.StockService.getStocks() throws com.google.gwt.sample.stockwatcher.client.NotLoggedInException' threw an unexpected exception: java.lang.NullPointerException: Name is null

Let me know if you encounter the same problem or not. The fact that it works in Google App Engine seems to indicate a bug in Hosted Mode.

Brunei answered 7/6, 2009 at 21:7 Comment(3)
For others to comment on: what about the case where you want a GAE-generated JDO id (@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)) of Key or Long? Can that be in a client-side object? If not, how do we identify client-side objects uniquely?Odell
Good question dfrankow. I had to handle this in a different project, so I modified the code above so that the ID is returned to the StockClient class. The basic idea is that the addStock method of StockServiceImpl needs to be modified to return the ID as a Long, and then code needs to be added to update the ID of the StockClient class.Brunei
This is the DTO approach. I don't like that pattern as it forces the developer to mantain two classes for a single entity. But sadly it seems to be the easier way to make persistable objects to work with GWT...Husking
T
4

GWT needs the .java file in addition to the .class file. Additionally, Stock needs to be in the "client" location of a GWT module.

Tyrannize answered 6/6, 2009 at 4:40 Comment(0)
H
3

The GWT compiler doesn't know about Stock, because it's not in a location it looks in. You can either move it to the client folder, or if it makes more sense leave it where it is and create a ModuleName.gwt.xml that references any other classes you want, and get your Main.gwt.xml file to inherit from that.

eg: DomainGwt.gwt.xml

<module>
    <inherits name='com.google.gwt.user.User'/>
    <source path="javapackagesabovethispackagegohere"/>
</module>

and:

<module rename-to="gwt_ui">
    <inherits name="com.google.gwt.user.User"/>
    <inherits name="au.com.groundhog.groundpics.DomainGwt"/>

    <entry-point class="au.com.groundhog.groundpics.gwt.client.GPicsUIEntryPoint"/>
</module>
Hyperform answered 6/6, 2009 at 4:51 Comment(1)
I'm having some trouble following your answer, rustyshelf - Are you saying I should create Stock.gwt.xml inside com.google.gwt.sample.stockwatcher and then inherit it inside StockWatcher.gwt.xml? I tried that, with <source path = "com.google.gwt.sample.stockwatcher.server"/>, but it didn't seem to help. I can't seem to find any documentation about how the gwt.xml files work.Brunei
C
1

There's a better answer here: GWT Simple RPC use case problem : Code included

Basically, you can add parameters to your APPNAME.gwt.xml file so the compiler to give the compiler a path to the server-side class.

Clubwoman answered 21/7, 2010 at 1:25 Comment(0)
H
1

I was getting the same issue and the "mvn gwt:compile" output was not very helpful. Instead, when I tried deploying to tomcat (via the maven tomcat plugin: mvn tomcat:deploy) I got helpful error messages.

A few things I had to fix up:

  1. Make the object that is sent from the client to the server implement Serializable
  2. Add an empty-arg constructor to that same object
Halm answered 12/10, 2010 at 11:35 Comment(0)
W
0

Yes, it is sure that we need to use the Serialization for getting the server objects to the client. These modile?? file settings won't work to use the Stock class in the client side.

In your case you have only one class Stock and you can create a StockClient in client side. It is easy. But what will be the solution if anyone having more classes. Something like the properties of this class are also some other type of classes.

Example: stock.getEOD(date).getHigh();

getEOD will return another class with the given date and that class has the getHigh method.

What to do in such big cases? I don't think creating all classes implementing serialization in client side is good for that. Then we have to write code in both server and client. all classes two times.

Woolson answered 19/8, 2009 at 14:0 Comment(0)
B
0

Keying off of rustyshelf's answer above ...

In my case I needed to edit the ModuleName.gwt.xml file and add the following:

<source path='client'/>
<source path='shared'/>

I created my project with the New->Web Application Project wizard but unchecked the Generate project sample code option. I then created the shared package. Had I not unchecked that, the package would have been created for me and the xml file modified per the above.

Brittni answered 3/2, 2012 at 21:2 Comment(0)
S
0

There is a far more simple and easy solution for that. If you want to send an object of your custom designed class from server side to client side you should define this custom class in shared package.

For example for your case the you just have to carry the Stock.java class (by drag and drop) into

com.google.gwt.sample.stockwatcher.shared

package. However from your package hierarchy screenshot it seems that you had deleted this shared package. Just re-create this package and drop the Stock.java inside it and let the game begin.

Severance answered 7/2, 2014 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.