我尝试使用此示例并学习如何调用休息服务, 当我把这段代码放入时,我得到了错误,因为日食并不知道 org.example.Customer; 。客户客户定义中的错误。 如何将其导入我的项目? 我试图使用日食建议但没有成功。 (修复项目设置等)
import java.util.List;
import javax.ws.rs.core.MediaType;
import org.example.Customer;
import com.sun.jersey.api.client.*;
public class JerseyClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource =
client.resource("http://localhost:8080/CustomerService/rest/customers");
// Get response as String
String string = resource.path("1")
.accept(MediaType.APPLICATION_XML)
.get(String.class);
System.out.println(string);
// Get response as Customer
Customer customer = resource.path("1") ------"*Here is the error in customer*-------
.accept(MediaType.APPLICATION_XML)
.get(Customer.class);
System.out.println(customer.getLastName() + ", "+ customer.getFirstName());
// Get response as List<Customer>
List<Customer> customers = resource.path("findCustomersByCity/Any%20Town")
.accept(MediaType.APPLICATION_XML)
.get(new GenericType<List<Customer>>(){});
System.out.println(customers.size());
}
}
我使用过这个博客的代码
http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-55.html
答案 0 :(得分:0)
如果您的org.example.Customer
课程不在同一个项目中,您可以将其包含在当前项目的类路径中。
请确保您的目录结构如下所示(从问题中给出的链接中使用它)。如果不喜欢它,则可能是错误的原因。
答案 1 :(得分:0)
可能是
1:导入语句import org.example.Customer;
是否以红色下划线?
2:班级Customer
是一个罐子里面的那个?或您工作区中的其他项目?或者它在哪里?
3:您的类路径中是否有可能有两个包含类Customer
的jar?
4:如果它在项目中,你是否首先构建了该项目?
更新,请参阅link here
他们已经展示了打包和部署组织,请参阅Customer类位于CustomerService.jar
内的CustomerService.war
内。你有这些东西吗?
老兄,你是否顺着教程?
答案 2 :(得分:0)