我正在尝试实现由以下Eclipse Java代码(在Ubuntu下)指定的非常简单的指定为here in section 6.5的客户端:
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的Servers列表中看到它,并且已经在同一教程的6.1-6.4节中运行了该示例。我还将所有Jersey JARS添加到WEB-INF / lib。我确保要启动一个与其他项目分开的新的Dynamic Web项目。
但是,我不断收到错误消息
The import com.sun.jersey cannot be resolved.
我知道Eclipse无法解析必要的软件包,但是如何使Eclipse了解这些软件包?我已经下载了jaxrs-ri-2.9.zip
,并将JARS复制到WEB-INF / lib目录。
我正在使用Eclipse Kepler。
EDIT:在项目“ Properies / Java Build path”下添加JAR文件也不起作用。
[非常好video tutorial,我取得了更好的成功。
我正在使用Eclipse Kepler。不过,请务必先通过“帮助/ Eclipse Marketplace ...”菜单安装JBoss。您还将需要所需的Jersey archive。
通过上述教程,我现在认为原来的问题是由于我没有安装正确的JAR文件。我最初使用的是bundle zip on this web page,但实际上您应该使用发现的here, as stated above的JAR。
这是因为您使用的源代码是Jersey 1.x API,但是您正在使用2.x API。 (现在)在本教程中已说明。
非常感谢您的帮助,并节省了很多时间。
我刚刚下载了**http://repo1.maven.org/maven2/com/sun/jersey/jersey-archive/1.19/**
解压缩并放入META-INF-> lib文件夹即可解决问题。