Search code examples
javaeclipserestjersey

The type javax.ws.rs.core.MediaType cannot be resolved


I am developing a Rest Client test program. I am new to this. From net I downloaded jersey-client-1.19.4.jar and added to Build Path in the eclipse. Every thing is fine except that webResource.accept gives below error.

The type javax.ws.rs.core.MediaType cannot be resolved

I am referring to REST Tutorials mentioned in below URL as reference for my test program development.

REST Client Tutorial

Below is my code: Kindly help

import java.io.*;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class Test {

public static void main(String[] args) throws IOException{
    // TODO Auto-generated method stub
    System.out.println("File Name: "+args[0]);
    String fileName = args[0];

    Client client = Client.create();
    WebResource webResource = client.resource("http://services.groupkt.com/country/get/iso2code/AX");
    ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);

    if (response.getStatus() != 200) 
    {
       throw new Exception("Exception Occured - HTTP Error Code : "
        + response.getStatus());
    }

    String output = response.getEntity(String.class);

    System.out.println("Fetching Output....");
    System.out.println(output);
}

}


Solution

  • I downloaded Jersey Client 1.19.4 from the Maven Repository

    and I got the same problem as you.

    This is how I overcome the problem:

    If your project is not maven, convert it to maven. Then go to pom.xml and add this:

    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.4</version>
    </dependency>
    

    to your <dependencies> tag.