0

我是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)

谢谢。

4

2 回答 2

0

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

     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),您必须使用服务器的相对路径。

再次感谢你的帮助。

于 2011-04-21T09:14:37.493 回答
0

除了发布的评论之外,您是如何启动 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访问它。

希望它可以帮助你。蒂埃里

于 2011-04-21T07:50:31.723 回答