“Not Found (404)” error in restlet

◇◆丶佛笑我妖孽 提交于 2019-12-11 23:27:18

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/5728004/not-found-404-error-in-restlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!