简体   繁体   English

Restlet ServerResource方法参数?

[英]Restlet ServerResource method parameters?

This is probably a really stupid/simple question with such an obvious answer that it seems not worth stating in restlet documentation. 这可能是一个非常愚蠢/简单的问题,有一个明显的答案,似乎不值得在restlet文档中说明。 Where and how (if at all) can Restlet pass parameters to methods in ServerResource classes? Restlet如何以及如何(如果有的话)将参数传递给ServerResource类中的方法?

Given this class: 鉴于此类:

public class FooServerResource extends ServerResource {
    @Get
    public String foo(String f) {
        return f;
    }
}

and a Router attachment router.attach("/foo", FooServerResource.class); 和路由器附件router.attach("/foo", FooServerResource.class);

I know that if I use a Restlet client connector I could create a proxy for this class and invoke methods directly, but what if I am making calls to this ServerResource from some other non-java language, eg PHP? 我知道如果我使用Restlet客户端连接器,我可以为这个类创建一个代理并直接调用方法,但是如果我从其他非Java语言(如PHP)调用此ServerResource会怎样?

You can access the query parameters using from the resource reference. 您可以使用资源引用访问查询参数。 Typically, something like this: 通常,这样的事情:

@Get
public String foo() {
    Form queryParams = getReference().getQueryAsForm();
    String f = queryParams.getFirstValue("f");
    return f;
}

Generally speaking (and this would work for other methods that GET ), you can access whatever is passed to the request (including the entity, when appropriate) using getRequest() within the ServerResource . 一般来说(这适用于GET其他方法),您可以使用ServerResource getRequest()访问传递给请求的任何内容(包括适当的实体getRequest()

hi

the question is about one year old, but I just started with Restlet and stumbled into the "same" problem. 这个问题大概是一岁,但我刚开始使用Restlet并且偶然发现了“同样”的问题。 I am talking about the server, not the client (as Bruno marked it, the original question is mixing server and client part) 我说的是服务器,而不是客户端(正如Bruno标记的那样,最初的问题是混合服务器和客户端部分)
I think the question is not completely answered. 我认为问题没有得到完全解答。 If you, for instance, prefer to separate the Restlet resource from semantic handling of the request (separating business logics from infrastructure) it is quite likely that you need some parameters, like an Observer, or a callback, or sth. 例如,如果您更喜欢将Restlet资源与请求的语义处理(将业务逻辑与基础结构分离)分开,则很可能需要一些参数,如Observer或回调,或者某些参数。 else. 其他。 So, as I fas as I see, no parameter could be transmitted into this instantiation process. 所以,正如我所看到的那样,没有参数可以传输到这个实例化过程中。 The resource is instantiated by Restlet engine per request. 资源由每个请求的Restlet引擎实例化。 Thus I found no way to pass a parameter directly (is there one?) 因此我发现无法直接传递参数(是否有一个?)

Fortunately it is possible to access the Application object of the Restlet engine from within the resource class, and thus also to the class that creates the component, the server, etc. 幸运的是,可以从资源类中访问Restlet引擎的Application对象,从而也可以访问创建组件,服务器等的类。

In the resource class I have sth. 在资源类我有...... like this 像这样

protected Application initObjLinkage(){

    Context cx = this.getContext();
    Client cli = cx.getClientDispatcher();
    Application app = cli.getApplication() ;
    return app;
}

Subsequently you may use reflection and an interface to access a method in the Application class (still within the resource class), check about this... 随后您可以使用反射和接口来访问Application类中的方法(仍在资源类中),检查有关此的 ...

Method cbMethod = app.getClass().getMethod("getFoo", parameterTypes) ;
CallbackIntf getmethodFoo = ( CallbackIntf )cbMethod.invoke( app, arguments );
String str = getmethodFoo()

In my application I use this mechanism to get access to an that supplies the received data to the classes for the business logics. 在我的应用程序中,我使用此机制来访问一个 ,该将接收到的数据提供给业务逻辑的类。 This approach is a standard for all my resource classes which renders them quite uniform, standardized and small. 这种方法是我所有资源类的标准,它使它们非常统一,标准化和小。
So... I just hope this is being helpful and that there is {no/some} way to do it in a much more simple way :) 所以...我只是希望这是有用的,并且有{not / some}方式以更简单的方式来做:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM