Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:49:29+00:00 2026-06-17T16:49:29+00:00

Using NetBeans and GlassFish, I’m developing a simple standalone application that gets a list

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:49:30+00:00Added an answer on June 17, 2026 at 4:49 pm

    Ok, there are some issues with your client code:

    Problem 1

    You are confusing media type with the response entity type. The Jersey WebResource class expects that when you use the method get, 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 an Accept header, via the accept method (which you are doing in your getTweets method, but not getUser).

    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:

    public class RestClient {
        private Client client;
        private WebResource webResource;
    
        public RestClient() {
            super();
        }
    
        public User getUser(String userName) throws UniformInterfaceException {
            final WebResource userResource = webResource
               .path(String.format("/user/%s", userName))
               .accept(MediaType.TEXT_XML) ;
            return userResource.get(User.class);
        }
    
        public Collection<Tweet> getTweets(String userName) throws UniformInterfaceException {
            final WebResource tweetResource = webResource
               .path(String.format("/user/%s/tweets", userName))
               .accept(MediaType.TEXT_XML) ;
            return tweetResource.get(new GenericType<Collection<Tweet>>(){});
        }
    }
    

    You will notice I added a user prefix 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:

    @Path("/rest")
    public class RESTService {
    
        @Inject
        KwetterService service;
    
        @GET
        @Path("/user/{userName}")
        public User getUser(@PathParam("userName") String userName)
        {
            return service.findByName(userName);
        }
    
        @GET
        @Path("/user/{userName}/tweets")
        @Produces(MediaType.TEXT_XML)
        public Collection<Tweet> getTweets(@PathParam("userName") String userName)
        {
            User user = service.findByName(userName);
            return user.getTweets();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently writing a java REST Web Service using Netbeans and Glassfish. The
I have a jax-ws web service developped using netbeans 7 and glassfish 3.1.2 .
I'm using Netbeans 6.5 to generate the JAX-WS Metro service and Glassfish 2.1 as
have a weird situation. I'm using Glassfish server for my Enterprise application. In that
I'm doing my first Java EE web application using glassfish and netbeans. When I
I'working on a enterprise application that uses JSF 2.0, with Netbeans 7.0 and Glassfish
I am working on a JavaEE6 application using Netbeans 7, Glassfish 3.1. The application
I'm using NetBeans IDE 7.0.1 and Glassfish 3.0.1. I'm building a J2EE application with
I am developing a Java EE enterprise application using NetBeans IDE. I can develop
I'm using NetBeans and GlassFish 3.0.1 to create an EJB3 application. I have written

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.