3

I'm trying to implement the very simple client specified here in section 6.5 and given by the following Eclipse Java code (under Ubuntu):

package de.vogella.jersey.first.client;

import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class Test {
    public static void main(String[] args) {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());
    // Fluent interfaces
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
    // Get plain text
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class));
    // Get XML
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));
    // The HTML
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_HTML).get(String.class));

  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/de.vogella.jersey.first").build();
  }
}

I've installed the Tomcat server, which I think is working because I can see it in the Servers list of Eclipse, and I've run the example in sections 6.1-6.4 of the same tutorial. I've also added all the Jersey JARS to WEB-INF/lib. I've made sure to start a new Dynamic Web project separate from anything else.

However, I keep getting the error

The import com.sun.jersey cannot be resolved.

I know Eclipse can't resolve the necessary packages, but how do I make Eclipse know about these? I'd already downloaded jaxrs-ri-2.9.zip and copied the JARS to the WEB-INF/lib directory.

I'm using Eclipse Kepler.

EDIT: Adding the JAR files under the project "Properies / Java Build path" also doesn't work.

  • 1
    Must be an issue with the tutorial. I see the same problem in Luna. – nitind Jul 01 '14 at 14:15
  • eclipse build path.[jersey jars](http://repo1.maven.org/maven2/com/sun/jersey/){client, core, server} `ClientResponse response = service.accept(MediaType.TEXT_PLAIN).get(ClientResponse.class); if (response.getStatus() == 200) status = response.getEntity(String.class); ` – Yash Dec 29 '15 at 06:42

2 Answers2

3

I'm having better success with this very good video tutorial.

I'm using Eclipse Kepler. Make sure to install JBoss via the "Help/Eclipse Marketplace..." menu first though. You will also need the desired Jersey archive.

By following the above tutorial, I now believe the original problem was due to the fact I didn't have the correct JAR files installed. I originally used the bundle zip on this web page, but in fact you should use the JARs found here, as stated above.

2

It is because the source you use is for the Jersey 1.x API, but you are using the 2.x API. That is (now) stated in the tutorial.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user1337
  • 460
  • 6
  • 22