编译调用休息服务的代码时出错

时间:2013-01-02 09:05:26

标签: java rest packages

我尝试使用此示例并学习如何调用休息服务, 当我把这段代码放入时,我得到了错误,因为日食并不知道 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

3 个答案:

答案 0 :(得分:0)

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

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

enter image description here

答案 1 :(得分:0)

可能是

1:导入语句import org.example.Customer;是否以红色下划线?

2:班级Customer是一个罐子里面的那个?或您工作区中的其他项目?或者它在哪里?

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

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

更新,请参阅link here

enter image description here

他们已经展示了打包和部署组织,请参阅Customer类位于CustomerService.jar内的CustomerService.war内。你有这些东西吗?

老兄,你是否顺着教程?

答案 2 :(得分:0)

您提供给link的博客说:

  

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

你在哪一步?

您是否遵循了所有步骤?