Eclipse 找不到 Jersey 进口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24512031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse can't find Jersey imports
提问by Pixel
I'm trying to implement the very simple client specified here in section 6.5and given by the following Eclipse Java code (under Ubuntu):
我正在尝试实现第 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();
}
}
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.
我已经安装了 Tomcat 服务器,我认为它可以工作,因为我可以在 Eclipse 的服务器列表中看到它,并且我已经运行了同一教程的 6.1-6.4 节中的示例。我还将所有 Jersey JARS 添加到 WEB-INF/lib。我已经确保启动一个与其他任何项目分开的新动态 Web 项目。
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.
我知道 Eclipse 无法解析必要的包,但是我如何让 Eclipse 知道这些?我已经下载jaxrs-ri-2.9.zip
并复制了 JARS 到 WEB-INF/lib 目录。
I'm using Eclipse Kepler.
我正在使用 Eclipse 开普勒。
EDIT:Adding the JAR files under the project "Properies / Java Build path" also doesn't work.
编辑:在项目“Properies / Java Build path”下添加 JAR 文件也不起作用。
采纳答案by Pixel
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.
我正在使用 Eclipse 开普勒。不过,请确保首先通过“帮助/Eclipse Marketplace...”菜单安装 JBoss。您还需要所需的泽西档案。
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.
按照上面的教程,我现在相信最初的问题是由于我没有安装正确的 JAR 文件。我最初在此网页上使用了bundle zip,但实际上您应该使用此处找到的 JAR ,如上所述。
回答by user1337
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.
这是因为您使用的源是 Jersey 1.x API,而您使用的是 2.x API。这是(现在)在教程中说明的。