无法解析类型 javax.ws.rs.core.MediaType

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

我正在开发一个 Rest Client 测试程序。我是新来的。我从网上下载了 jersey-client-1.19.4.jar 并添加到 Eclipse 中的构建路径。一切都很好,除了 webResource.accept 给出以下错误。

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

我指的是下面提到的 REST 教程 URL 作为我的测试程序开发的参考。

REST Client Tutorial

下面是我的代码:请帮忙

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);
}

}

我从 https://mvnrepository.com/artifact/com.sun.jersey/jersey-client/1.19.4

下载了 Jersey Client 1.19.4

我遇到了和你一样的问题

我是这样解决问题的:

如果您的项目不是maven,请将其转换为maven。 然后转到 pom.xml 并添加:

<!-- 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>

到您的 <dependencies> 标签。

@yoav 的解决方案很棒,但是如果有人有同样的问题并且不想/不需要转换为 maven(这对依赖管理更好),您只需要包含 zip 文件中的所有文件你发现跟随这个 link.

您会发现:

  • jersey-client-1.19.4.jar
  • jersey-core-1.19.4.jar
  • jsr311-api-1.1.1.jar

将它们全部放入你的库中。