Eclipse 找不到 Jersey 导入

标签 eclipse web-services rest tomcat7 jersey-client

我正在尝试实现指定的非常简单的客户端 here in section 6.5并由以下 Eclipse Java 代码给出(在 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();
  }
}

我已经安装了 Tomcat 服务器,我认为它正在工作,因为我可以在 Eclipse 的服务器列表中看到它,并且我已经运行了同一教程第 6.1-6.4 节中的示例。我还将所有 Jersey JARS 添加到 WEB-INF/lib 中。我已确保启动一个与其他任何项目分开的新动态 Web 项目。

但是,我不断收到错误

The import com.sun.jersey cannot be resolved.

我知道 Eclipse 无法解析必要的包,但如何让 Eclipse 了解这些包?我已经下载了 jaxrs-ri-2.9.zip 并将 JARS 复制到 WEB-INF/lib 目录。

我正在使用 Eclipse Kepler。

编辑:在项目“Properies/Java Build path”下添加 JAR 文件也不起作用。

最佳答案

我通过这个非常好的东西取得了更好的成功video tutorial .

我正在使用 Eclipse Kepler。确保首先通过“Help/Eclipse Marketplace...”菜单安装 JBoss。您还需要所需的 Jersey archive .

通过遵循上述教程,我现在相信最初的问题是由于我没有安装正确的 JAR 文件所致。我最初使用bundle zip on this web page ,但实际上您应该使用找到的 JAR here, as stated above .

关于Eclipse 找不到 Jersey 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24512031/

相关文章:

rest - REST API 设计中的查找或创建习惯用法?

java - Java SAX 解析器中的变音

java - Glassfish Web服务高并发

.net - 使用我的本地 SSL 证书配置端口

node.js - 在 REST API 中合并 2 个查询的输出

web-services - SOAP 的末日即将来临吗?

Android - apk 签名 : entries' certificate chain is not validated & signatures w/o a timestamp

java - Eclipse 无法启动,日志错误仅显示 : ! SESSION !SESSION

php - 我可以在不使用 Apache 的情况下在 Eclipse PDT 下调试 PHP 文件吗?

java - Axis - 响应类扩展了 AxisFault 并被抛出而不是返回?