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

xwbd5t1u  于 2022-09-21  发布在  Java
关注(0)|答案(2)|浏览(365)

我正在开发一个REST客户端测试程序。我对此还是个新手。我从Net下载了Jersey-Client-1.19.4.jar并添加到了eclipse中的构建路径中。除了webResource.Accept给出下面的错误之外,其他一切都很好。
无法解析类型javax.ws.rs.core.MediaType

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

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

}

n1bvdmb6

n1bvdmb61#

@Yoav提供的解决方案很棒,但如果有人有同样的问题并且不想/需要转换为maven(这对于依赖项管理更好),您只需要包含link后面的压缩文件中的所有文件。

您将发现:

  • Jersey-客户端-1.19.4.jar
  • Jersey-core-1.19.4.jar
  • jsr311-api-1.1.1.jar

把它们都放进你的库里。

7eumitmz

7eumitmz2#

我从Maven Repository下载了Jersey客户端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>标签。

相关问题