【问题标题】:Restlet 2.1 application has null contextRestlet 2.1 应用程序具有空上下文
【发布时间】:2012-12-05 04:10:26
【问题描述】:

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

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 始终为空,因此我相信参数和属性会在 ServerResource 中丢失。

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

如果它不是一个错误,那么我如何在这种应用程序中获得一个像样的上下文???

请高人指教。

【问题讨论】:

    标签: restlet restlet-2.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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2016-08-27
      • 2011-05-25
      • 2015-08-31
      • 1970-01-01
      • 2015-11-12
      • 2016-05-20
      相关资源
      最近更新 更多