问题
This question is an extension of previous question. GWT Cannot compile the project
Please have a look and tell me what's wrong with it.
Client side.
Twitter.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class Twitter implements EntryPoint {
private final TwitterServiceAsync searchService = GWT.create(TwitterService.class);
public void onModuleLoad() {
// Create a Flex Table
final FlexTable flexTable = new FlexTable();
FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
flexTable.addStyleName("cw-FlexTable");
flexTable.setWidth("32em");
flexTable.setCellSpacing(5);
flexTable.setCellPadding(3);
// Add some text
cellFormatter.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
cellFormatter.setColSpan(0, 0, 2);
searchService.search("love", new AsyncCallback<ArrayList<Tweet>>() {
@Override
public void onFailure(Throwable caught) {
//Not yet completed.
}
@Override
public void onSuccess(ArrayList<Tweet> result) {
for (int i=0 ; i <result.size() ; i++) {
Tweet tweet = result.get(i);
String user = tweet.getFromUser();
String status = tweet.getText();
addRow(flexTable, user);
addRow(flexTable, status);
}
}});
flexTable.ensureDebugId("cwFlexTable");
RootPanel.get().add(flexTable);
}
/**
* Add a row to the flex table.
*/
private void addRow(FlexTable flexTable, String status) {
int numRows = flexTable.getRowCount();
flexTable.setText(numRows, 0, status);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
}
/**
* Remove a row from the flex table.
*/
private void removeRow(FlexTable flexTable) {
int numRows = flexTable.getRowCount();
if (numRows > 1) {
flexTable.removeRow(numRows - 1);
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows - 1);
}
}
}
TwitterService.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("search")
public interface TwitterService extends RemoteService {
ArrayList<Tweet> search(String searchTerm) throws IllegalArgumentException;
}
TwitterServiceAsync.java
package in.isuru.twitter.client;
import java.util.ArrayList;
import twitter4j.Tweet;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface TwitterServiceAsync {
void search(String searchTerm, AsyncCallback<ArrayList<Tweet>> callback) throws llegalArgumentException;
}
Server Side
package in.isuru.twitter.server;
import java.util.ArrayList;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import in.isuru.twitter.client.TwitterService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class TwitterServiceImpl extends RemoteServiceServlet implements TwitterService {
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
@Override
public ArrayList<Tweet> search(String searchTerm) {
// The factory instance is re-useable and thread safe.
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query(searchTerm);
QueryResult result = null;
try {
result = twitter.search(query);
} catch (TwitterException e) {
e.printStackTrace();
}
for (Tweet tweet : result.getTweets()) {
//System.out.println(tweet.getFromUser() + ":" + tweet.getText());
tweets.add(tweet);
}
return tweets;
}
}
Twitter.gwt.xml
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='twitter'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User'/>
<!-- Inherit the default GWT style sheet. You can change -->
<!-- the theme of your GWT application by uncommenting -->
<!-- any one of the following lines. -->
<inherits name='com.google.gwt.user.theme.clean.Clean'/>
<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->
<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->
<!-- Other module inherits -->
<!-- Specify the app entry point class. -->
<entry-point class='in.isuru.twitter.client.Twitter'/>
<!-- Specify the paths for translatable code -->
<source path='client'/>
<source path='shared'/>
</module>
Error is
[DEBUG] [twitter] - Validating newly compiled units [TRACE] [twitter]
- Finding entry point classes [ERROR] [twitter] - Errors in 'file:/E:/workspace/Twitter/src/in/isuru/twitter/client/Twitter.java'
[ERROR] [twitter] - Line 43: No source code is available for type
twitter4j.Tweet; did you forget to inherit a required module? [ERROR]
[twitter] - Errors in
'file:/E:/workspace/Twitter/src/in/isuru/twitter/client/TwitterServiceAsync.java'
[ERROR] [twitter] - Line 11: No source code is available for type
twitter4j.Tweet; did you forget to inherit a required module? [ERROR]
[twitter] - Unable to find type 'in.isuru.twitter.client.Twitter'
[ERROR] [twitter] - Hint: Previous compiler errors may have made
this type unavailable [ERROR] [twitter] - Hint: Check the
inheritance chain from your module; it may not be inheriting a
required module or a module may not be adding its source path entries
properly [ERROR] [twitter] - Failed to load module 'twitter' from user
agent 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like
Gecko) Chrome/16.0.912.77 Safari/535.7' at 127.0.0.1:7901
回答1:
class twitter4j.Tweet
cannot be used in client code since it is not included in any inherited GWT module. You need to create your own transfer object class, for example TweetDTO
somewhere in your client
or shared
packages. Only this object should be used in interfaces TwitterServiceAsync
and TwitterService
. Then in your servlet, you create TweetDTO
copy required values from Tweet
to TweetDTO
and then return a list of TweetDTO
.
来源:https://stackoverflow.com/questions/9122239/gwt-rpc-cannot-compile-the-project