Using NetBeans and GlassFish,
I’m developing a simple standalone application that gets a list of Tweet‘s from a REST service. Those objects contain, amongst others, a variable called tweet, wich is the String-value I’d like to see in the other application.
I’ve built the website application and created a new class in this:
RESTService.java:
package service;
import domain.Tweet;
import domain.User;
import java.util.Collection;
import java.util.Date;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/rest")
public class RESTService {
@Inject
KwetterService service;
@GET
@Path("/{user}")
public User getUser(@PathParam("user") String userName)
{
return service.findByName(userName);
}
@GET
@Path("/{user}/tweets")
@Produces(MediaType.TEXT_XML)
public Collection<Tweet> getTweets(@PathParam("user") String userName)
{
User user = service.findByName(userName);
return user.getTweets();
}
}
In the standalone application, I’ve created a small form with a button. With a press on that button, I’d like to grab those tweets.
When using NetBeans to add a REST client class, I linked it to the service of the website. It generated a class I called RestClient.java:
package service;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
public class RestClient {
private WebResource webResource;
private Client client;
public RestClient() {
}
public <T> T getUser(Class<T> responseType, String user) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{user}));
return resource.get(responseType);
}
public <T> T getTweets(Class<T> responseType, String user) throws UniformInterfaceException {
WebResource resource = webResource;
resource = resource.path(java.text.MessageFormat.format("{0}/tweets", new Object[]{user}));
return resource.accept(javax.ws.rs.core.MediaType.TEXT_XML).get(responseType);
}
}
In the actionbutton performed method, I’m trying to use that class to gain tweets. When doing so, it asks for a Class<T> responseType next to the username on which it should get tweets on.
The code of the form looks like this:
private void btnZoekFollowersActionPerformed(java.awt.event.ActionEvent evt) {
String userName = txtZoekTweets.getText(); //de naam van de user
RestClient client = new RestClient();
Collection<Tweet> tweets = client.getTweets(MediaType.TEXT_XML, userName);
//What to insert for mediatype? Above gives an error since it requires Class<T> responseType
System.out.println("Datasize: " + data.size());
}
So, in short, my question is as follows: I need to insert a Class<T> responseType as parameter, why? And what should I use as parameter or, better, how can I avoid having to do this and instead only insert the username as parameter?
And next to this, do I need to type code myself somewhere where I specify which URL to approach? Or is this all set and working through generating the client-class?
Ok, there are some issues with your client code:
Problem 1
You are confusing media type with the response entity type. The Jersey
WebResourceclass expects that when you use the methodget, that you supply the expected entity type that the response should be marshaled into. If you want to specify the expected media type then you need to do that by appending anAcceptheader, via theacceptmethod (which you are doing in yourgetTweetsmethod, but notgetUser).Problem 2
Your REST client class does not need to have parameterized generic functions. You have already defined your methods to be aware of what kind of object they are returning, so use those objects in the method signature!
Solution
Overall I would expect your client to look like this:
You will notice I added a
userprefix to the resource URL’s. You can remove it if you wish, but it will make it easier to maintain your application as you add more resource endpoints. If you decide to keep it, then you will need this modification on your server side: