【问题标题】:"Not Found (404)" error in restletrestlet 中的“未找到 (404)”错误
【发布时间】:2011-08-09 08:07:08
【问题描述】:

我是 restlet 框架的新手。 我创建了一个小型 java ee 应用程序,但它给了我一个错误“未找到 (404)”

公共类 MailServerApplication 扩展应用程序 { @覆盖 公共 Restlet createInboundRoot() { 路由器 router = new Router(getContext()); router.attach("http://localhost:8084/accounts/{accountId}/mails/{mailId}", MailServerResource.class); 返回路由器; } } //////////////////////////// 公共类 MailServerResource 扩展 ServerResource { @覆盖 受保护的表示 get() 抛出 ResourceException { DomRepresentation 结果 = null; 尝试 { 结果 = 新的 DomRepresentation(); 结果.setIndenting(true); 文档 doc = result.getDocument(); 节点 mailElt = doc.createElement("mail"); doc.appendChild(mailElt); 节点状态Elt = doc.createElement("status"); statusElt.setTextContent("收到"); mailElt.appendChild(statusElt); 节点 subjectElt = doc.createElement("subject"); subjectElt.setTextContent("给自己的信息"); mailElt.appendChild(subjectElt); 节点 contentElt = doc.createElement("content"); contentElt.setTextContent("Doh!"); mailElt.appendChild(contentElt); } 捕捉(IOException e){ } 返回结果; } @覆盖 受保护的表示放(表示表示)抛出 ResourceException { DomRepresentation mailRep = new DomRepresentation(representation); 文件文件; 尝试 { doc = mailRep.getDocument(); 元素 mailElt = doc.getDocumentElement(); 元素 statusElt = (元素) mailElt .getElementsByTagName("状态").item(0); 元素 subjectElt = (元素) mailElt.getElementsByTagName( "主题").item(0); 元素 contentElt = (元素) mailElt.getElementsByTagName( “内容”).item(0); 元素 accountRefElt = (元素) mailElt.getElementsByTagName( "accountRef").item(0); System.out.println("状态:" + statusElt.getTextContent()); System.out.println("主题:" + subjectElt.getTextContent()); System.out.println("内容:" + contentElt.getTextContent()); System.out.println("账户地址:" + accountRefElt.getTextContent()); } 捕捉(IOException e){ 抛出新的资源异常(e); } 返回空值; } }

但如果我运行/调试它。它给出了以下错误:

未找到线程“主”中的异常 (404) - 未找到 在 org.restlet.resource.ClientResource.handle(ClientResource.java:858) 在 org.restlet.resource.ClientResource.handle(ClientResource.java:763) 在 org.restlet.resource.ClientResource.get(ClientResource.java:496) 在 MailClient.main(MailClient.java:19)

谢谢。

【问题讨论】:

  • 这个错误意味着找不到服务器页面。您确定您输入了正确的 URL,并且可以访问吗?
  • 您使用什么 URL 访问它?此外,您的router.attach() 调用可能不应该指定所有host:port 的东西。相反,请尝试仅使用 router.attach("/accounts/{accountId}/mails/{mailId}")

标签: java rest restlet java-ee-5


【解决方案1】:

除了发布的 cmets,您是如何启动您的 Restlet 应用程序的?使用Server类,如下:

public class MailServerApplication extends Application {
  (...)
  public static void main(String[] args) {
    try {
      Server server = new Server(Protocol.HTTP, 8084);
      server.setNext(new MailServerApplication());
      server.start();

      System.out.println("Press a key to stop");
      System.in.read();
    } catch(Exception ex) {
      ex.printStackTrace();
    }
  }
}

正如您所说,您开发了一个 JavaEE 应用程序,也许您使用了 servlet 扩展?在这种情况下,servlet 级别的映射也可以考虑在内。

使用第一种方法,我使用 org.restlet.jar 和 org.restlet.ext.xml.jar(版本 2.0.5,jee 版)使您的应用程序工作。我使用 url http://localhost:8084/accounts/10/mails/1 访问它。

希望对您有所帮助。 蒂埃里

【讨论】:

    【解决方案2】:

    感谢河马。
    实际上问题出在网址中。
    我不得不修改以下行

    router.attach("http://localhost:8084/accounts/{accountId}/mails/{mailId}", MailServerResource.class);

    进入这一行。

    router.attach("/accounts/{accountId}/mails/{mailId}", MailServerResource.class);

    如果您使用 JavaSE 的 restlet 框架,那么第一个 url 就可以了。但是对于 Web 应用程序(java ee),您必须使用服务器的相对路径。

    再次感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 2015-09-19
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多