0

我尝试使用此示例并学习如何调用休息服务,当我输入此代码时出现错误,因为 eclipse 不知道 org.example.Customer; .定义客户客户的错误。如何将其导入我的项目?我尝试使用 eclipse 建议但没有成功。(修复项目设置等)

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

4

3 回答 3

0

如果您的org.example.Customer课程不在您创建此服务的同一个项目中,您可以将其包含到当前项目的类路径中。

只需确保您的目录结构如下所示(从您在问题中给出的链接中使用它)。如果它不喜欢它,那么这可能是错误的原因。

在此处输入图像描述

于 2013-01-02T09:10:53.393 回答
0

您提供链接的博客说:

在我们在第 4 部分中创建的 CustomerService 会话 bean 中,我们指定我们的 RESTful 服务将使用 @Path 注释的路径“/customers”。

你在哪一步?

您是否遵循了所有步骤?

于 2013-01-02T09:34:12.853 回答
0

可能

1:import语句import org.example.Customer;有红色下划线吗?

2:类Customer是在罐子里吗?或您工作区中的另一个项目?或者它在哪里?

3:是否有可能在您的类路径中有两个包含该类的 jar Customer

4:如果它在一个项目中,您是否首先构建了该项目?

更新,请参阅此处的链接

在此处输入图像描述

他们展示了打包和部署组织,看到 Customer 类在里面CustomerService.jar,里面在里面CustomerService.war。你有这些物品吗?

兄弟,你按照教程学的好吗?

于 2013-01-02T09:14:39.570 回答