java - 将上下文属性传递给 ServerResource

标签 java restlet restlet-2.0

我的应用程序尝试设置上下文属性:

    final Router router = new Router();
    router.attachDefault(HttpListener.class);

    org.restlet.Application myApp = new org.restlet.Application() {
        @Override
        public org.restlet.Restlet createInboundRoot() {
            getContext().getAttributes().put("mysharedobj", new MySharedObj());
            return router;
        };
    };
    Component component = new Component();
    component.getDefaultHost().attach("/", myApp);

    new Server(Protocol.HTTP, port, component).start();

在我的 HttpListener 中,我断言上下文不为空:

public class HttpListener extends ServerResource {

    public MySharedObj mysharedobj;

    public HttpListener() { }  

    @java.lang.Override
    public void init(Context context, Request request, Response response) {

        assert context != null;  // throws java.lang.AssertionError

        // my end goal is to pass a shared object to my listener
        mysharedobj = context.getAttributes().get("mysharedobj");
    }
    ...
}

但是,由于上下文为空,因此抛出 java.lang.AssertionError。我的最终目标是将共享对象传递给我的听众。还有其他方法吗?

我哪里错了?注意:我使用的是 ReSTLet 2.1.7。我的应用程序始终从 Android 应用程序运行,因此没有可用的服务器上下文。


更新:

我也尝试过使用应用程序上下文:

    final Router router = new Router();
    router.attachDefault(HttpListener.class);

    Component component = new Component();

    final Context ctx = component.getApplication().getContext().createChildContext();
    ctx.getAttributes().put("mysharedobj", new MySharedObj());

    org.restlet.Application myApp = new org.restlet.Application(ctx) {
        @Override
        public org.restlet.Restlet createInboundRoot() {
            return router;
        };
    };

还有..

public HttpListener() {
    Context ctx = getApplication().getContext();
    assert ctx.getAttributes().size() > 0;  // Throws AssertionError
    ...     
}

在这种方法中,我能够访问应用程序上下文,但由于某种原因未在其上设置属性。

最佳答案

从您的更新的 部分中删除final,然后它将起作用。因为您只能在 constructorinitializer 中设置 final 变量。在常规方法中,不能更改声明为 final 的变量的值。

所以,你的代码将是

 Router router = new Router(); // Remove final from this.
    router.attachDefault(HttpListener.class);

    Component component = new Component();

    Context ctx = component.getApplication().getContext().createChildContext(); // Remove final
    ctx.getAttributes().put("mysharedobj", new MySharedObj());

    org.restlet.Application myApp = new org.restlet.Application(ctx) {
        @Override
        public org.restlet.Restlet createInboundRoot() {
            return router;
        };
    };

您可以从 here 找到完整的源代码.

资源链接:

  1. Restlet Framework – Hello World Example
  2. Restlet Authorization

更新1:

来自 Restlet documentation and sample code ,我得到了一些有用的区域。希望对您有所帮助。

public class MyApiWithRoleAuthorization extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = createRouter();
        return router;
    }
    private Router createRouter() {
        //Attach Server Resources to given URL
        Router router = new Router(getContext());
        router.attach("/resource1/", Resource1.class);
        router.attach("/resource2/", Resource2.class);
        return router;
    }
      public static void main(String[] args) throws Exception {
        //Attach application to http://localhost:9000/v1
        Component c = new Component();
        c.getServers().add(Protocol.HTTP, 9000);
        c.getDefaultHost().attach("/v1", new MyApiWithRoleAuthorization());
        c.start();
    }
}

资源类,将它们命名为 Resource1、Resource2 等...并从此处复制粘贴它们的内容:

资源0.java

public class Resource0 extends ServerResource{

    @Get
    public String represent() throws Exception {
        return this.getClass().getSimpleName() + " found !";
    }

    @Post
    public String add() {
        return this.getClass().getSimpleName() + " posted !";
    }

    @Put
    public String change() {
        return this.getClass().getSimpleName() + " changed !";
    }

    @Patch
    public String partiallyChange() {
        return this.getClass().getSimpleName() + " partially changed !";
    }

    @Delete
    public String destroy() {
        return this.getClass().getSimpleName() + " deleted!";
    }
}

关于java - 将上下文属性传递给 ServerResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37680492/

相关文章:

Java/TCP 流量类

java - 带有 ReSTLet 的 PDFbox

reSTLet - 我可以在 ReSTLet 框架中关闭注销吗?

java - resque :failed and resque:stat:failed keys?有什么区别

java - Spark 展平数据集映射列

java - 如何通过 ReSTLet 使用 JSON Web 服务?

java - 如何创建 protected ReSTLet资源?

java - ReSTLet + 嵌入式 jetty + webapp

web-services - org.reSTLet : Posting JSON content against webservice returns HTTP error 411 (length required)

java - 是否可以确定从 Activity 发回的数据是 String 还是 String[]?