Search code examples
javarestrestletjava-ee-5

"Not Found (404)" error in restlet


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.


Solution

  • 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.