在restlet中出现“Not Found(404)”错误

时间:2021-08-23 20:14:14

i am new to restlet framework. i have created a small java ee application but it give me an error "Not Found (404)"

我是restlet框架的新手。我创建了一个小的java ee应用程序,但它给我一个错误“Not Found(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.

2 个解决方案

#1


0  

In addition to posted comments, how did you start your Restlet application? Using the Server class, as below:

除了发表的评论,你是如何开始你的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();
    }
  }
}

As you said, you develop a JavaEE application, perhaps did you use the servlet extension? In this case, mapping at servlet level can also enter into account.

正如您所说,您开发了一个JavaEE应用程序,也许您使用了servlet扩展?在这种情况下,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). I accessed it using url http://localhost:8084/accounts/10/mails/1.

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

Hope it helps you. Thierry

希望它能帮到你。蒂埃里

#2


0  

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. but for web application (java ee) you have to use relative path of the server.

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

thanks again for your help.

再次感谢你的帮助。

#1


0  

In addition to posted comments, how did you start your Restlet application? Using the Server class, as below:

除了发表的评论,你是如何开始你的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();
    }
  }
}

As you said, you develop a JavaEE application, perhaps did you use the servlet extension? In this case, mapping at servlet level can also enter into account.

正如您所说,您开发了一个JavaEE应用程序,也许您使用了servlet扩展?在这种情况下,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). I accessed it using url http://localhost:8084/accounts/10/mails/1.

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

Hope it helps you. Thierry

希望它能帮到你。蒂埃里

#2


0  

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. but for web application (java ee) you have to use relative path of the server.

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

thanks again for your help.

再次感谢你的帮助。