【问题标题】:error in compiling code that call to rest service编译调用休息服务的代码时出错
【发布时间】:2013-01-02 17:41:46
【问题描述】:

我尝试使用此示例并学习如何调用休息服务, 当我输入这段代码时,我收到错误,因为 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

【问题讨论】:

  • 仅供参考您的目录结构将帮助您获得答案

标签: java rest packages


【解决方案1】:

可能是

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

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

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

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

更新,见link here

他们展示了打包和部署组织,看到 Customer 类在 CustomerService.jar 内部,而在 CustomerService.war 内部。你有这些东西吗?

老兄,你按照教程很好吗?

【讨论】:

  • 1.是 2. 不确定 3. 不确定
  • 酷,红色下划线表示您指定的导入路径不正确,即那条路上什么都没有。现在告诉我..客户类在哪里?在你的工作区?哈哈@StefanStrooves?\
  • 没有这个类不在我的工作区我认为这个类来自 org.example.Customer 包,不是吗?
  • @StefanStrooves,是的,它即将到来。但是那个包裹在哪里?你知道,java不会自己在你的硬盘驱动器中寻找org.example.Customer,是的,也不是在互联网上,以防你只是在想.. ;),你需要告诉java,在哪里寻找必修课..查看我更新的帖子以获得另一个提示
【解决方案2】:

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

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

【讨论】:

  • 你现在可以指导我吗?
  • 既然我是 eclipse 新手,你能告诉我我应该如何检查它,因为我只将代码粘贴到新项目中的新类...
【解决方案3】:

你给link的博客说:

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

你在哪一步?

您是否遵循了所有步骤?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多