“Not Found (404)” error in restlet

2019-08-16 08:30发布

i am new to restlet framework. i have created a small java ee application but it give me an error "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条回答
聊天终结者
2楼-- · 2019-08-16 09:00

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

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.

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.

Hope it helps you. Thierry

查看更多
smile是对你的礼貌
3楼-- · 2019-08-16 09:06

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.

thanks again for your help.

查看更多
登录 后发表回答