Restlet 2.1应用程序具有空上下文

时间:2012-12-05 04:10:26

标签: restlet restlet-2.0

我正在使用Restlet 2.1运行JSE应用程序。我试图使用应用程序上下文,并发现它在我的应用程序中始终为null。因为它是null,我似乎无法访问任何东西 - 包括我在调用资源时传递的任何属性。

restlet应用程序类的代码如下:

package net.factor3.mailapp;

import net.factor3.mailapp.impl.PageServerImpl;

import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.restlet.routing.Template;

public class MyServer extends Application
{
   public MyServer()
   {
      setName("Test Application");
      setDescription("Testing use of Restlets");
   }

  @Override
  public Restlet createInboundRoot()
  {
      Context ctx = getContext();
      Router route = new Router(ctx);

      route.setDefaultMatchingMode(Template.MODE_EQUALS);
      route.attach("http://localhost:8100/",PageServerImpl.class);
      route.attach("http://localhost:8100/{page}",PageServerImpl.class);

      return(route);
   }

   public static void main(String[] args) throws Exception
   {
      Server asrv = new Server(Protocol.HTTP,8100);
      asrv.setNext(new MyServer());
      asrv.start();
   }

}

请注意,PageServerImpl是ServerResource。在createInboundRoot()中,我使用getContext()来获取应用程序的上下文并将其放入ctx。 ctx总是为null,我相信因为这个原因,ServerResource中的参数和属性都会丢失。

这是Restlet 2.1的JRE版本中的错误吗?如果是的话,我该去哪里举报? Restlet网站上的错误报告没有明确的链接。

如果它不是一个bug,那么如何在这种应用程序中获得一个合适的上下文呢?

有人请指教。

1 个答案:

答案 0 :(得分:1)

使用组件类,您可以创建满足您需求的信息:

 public class MyServer extends Application
 {
    public MyServer()
    {
       setName("Test Application");
       setDescription("Testing use of Restlets");
    }

    public static void main(String[] args) throws Exception
    {
        // Create a new Restlet component and add a HTTP server connector to it
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182); 
        // Then attach it to the local host
        component.getDefaultHost().attach("/trace", GenericResource.class); 
        // Now, let's start the component!
        // Note that the HTTP server connector is also automatically started.
        component.start();
    }
}