简体   繁体   English

Restlet中的“未找到(404)”错误

[英]“Not Found (404)” error in restlet

i am new to restlet framework. 我是Restlet框架的新手。 i have created a small java ee application but it give me an error "Not Found (404)" 我创建了一个小型的Java ee应用程序,但它给我一个错误“未找到(404)”

public class MailServerApplication extends Application {
   @Override
   public Restlet createInboundRoot() {
      Router router = new Router(getContext());
      router.attach("http://localhost:8084/accounts/{accountId}/mails/{mailId}", MailServerResource.class);
      return router;
   }
}

////////////////////////////////
public class MailServerResource extends ServerResource {
   @Override
   protected Representation get() throws ResourceException {

      DomRepresentation result = null;
      try {
         result = new DomRepresentation();
         result.setIndenting(true);
         Document doc = result.getDocument();
         Node mailElt = doc.createElement("mail");
         doc.appendChild(mailElt);
         Node statusElt = doc.createElement("status");
         statusElt.setTextContent("received");
         mailElt.appendChild(statusElt);
         Node subjectElt = doc.createElement("subject");
         subjectElt.setTextContent("Message to self");
         mailElt.appendChild(subjectElt);
         Node contentElt = doc.createElement("content");
         contentElt.setTextContent("Doh!");
         mailElt.appendChild(contentElt);
      } catch (IOException e) {
      }
      return result;
   }
   @Override
   protected Representation put(Representation representation) throws ResourceException {
      DomRepresentation mailRep = new DomRepresentation(representation);
      Document doc;
      try {
         doc = mailRep.getDocument();
         Element mailElt = doc.getDocumentElement();
         Element statusElt = (Element) mailElt
         .getElementsByTagName("status").item(0);
         Element subjectElt = (Element) mailElt.getElementsByTagName(
         "subject").item(0);
         Element contentElt = (Element) mailElt.getElementsByTagName(
         "content").item(0);
         Element accountRefElt = (Element) mailElt.getElementsByTagName(
         "accountRef").item(0);
         System.out.println("Status: " + statusElt.getTextContent());
         System.out.println("Subject: " + subjectElt.getTextContent());
         System.out.println("Content: " + contentElt.getTextContent());
         System.out.println("Account URI: " + accountRefElt.getTextContent());
      } catch (IOException e) {
         throw new ResourceException(e);
      }
      return null;
   }
}

but if i run/debug it. 但是,如果我运行/调试它。 it gives following error: 它给出以下错误:

Exception in thread "main" Not Found (404) - Not Found
        at org.restlet.resource.ClientResource.handle(ClientResource.java:858)
        at org.restlet.resource.ClientResource.handle(ClientResource.java:763)
        at org.restlet.resource.ClientResource.get(ClientResource.java:496)
        at MailClient.main(MailClient.java:19)

thanks. 谢谢。

In addition to posted comments, how did you start your Restlet application? 除了发布的评论,您还如何启动Restlet应用程序? Using the Server class, as below: 使用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();
    }
  }
}

As you said, you develop a JavaEE application, perhaps did you use the servlet extension? 如您所说,您开发了JavaEE应用程序,也许您使用了servlet扩展? In this case, mapping at servlet level can also enter into account. 在这种情况下,也可以考虑servlet级别的映射。

With the first approach, I made work your application with org.restlet.jar and org.restlet.ext.xml.jar (version 2.0.5, jee edition). 使用第一种方法,我使用org.restlet.jar和org.restlet.ext.xml.jar(版本2.0.5,jee版本)来处理您的应用程序。 I accessed it using url http://localhost:8084/accounts/10/mails/1 . 我使用url http:// localhost:8084 / accounts / 10 / mails / 1访问了它。

Hope it helps you. 希望对您有帮助。 Thierry 蒂埃里

hi thanks to hippo. 嗨,感谢河马。
actually the problem was in the url. 实际上问题出在网址中。
i had to modify following line 我不得不修改以下行

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

into this line. 进入这一行。

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

if you use the restlet framework for JavaSE then first url was ok. 如果您将restlet框架用于JavaSE,则第一个URL可以。 but for web application (java ee) you have to use relative path of the server. 但是对于Web应用程序(java ee),您必须使用服务器的相对路径。

thanks again for your help. 再次感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM