简体   繁体   English

在Restlet中是否可以使用强类型的HTTP请求处理程序?

[英]Is it possible to have strongly typed HTTP request handlers in Restlet?

Consider the following ServerResource derived type: 考虑以下ServerResource派生类型:

public class UserResource extends ServerResource {
  @Get
  public User getUser(int id) {
    return new User(id, "Mark", "Kharitonov");
  }
}

(Yes, it always returns the same user no matter the given id). (是的,无论给定的id总是返回相同的用户)。

Is it possible to make it work in Restlet? 是否可以使其在Restlet中工作? Because, as far as I understand, the expected signature of the GET handler is: 因为据我了解,GET处理程序的预期签名是:

Representation get();

OR 要么

Representation get(Variant v);  // (no idea what it means yet)

Now I understand, that I can implement the non type safe GET handler to extract the arguments from the request and then invoke getUser , after which to compose the respective Representation instance from the result and return. 现在我明白了,我可以实现非类型安全的GET处理程序以从请求中提取参数,然后调用getUser ,然后从结果中组成相应的Representation实例并返回。 But this is a boilerplate code, it does not belong with the application code, its place is inside the framework. 但这是一个样板代码,它不属于应用程序代码,它位于框架内部。 At least, this is how it is done by OpenRasta - the REST framework I have been using in .NET 至少,这是通过OpenRasta(我在.NET中一直在使用的REST框架)完成的

Thanks. 谢谢。

You should remove the parameter from the signature 您应该从签名中删除参数

  @Get
  public User getUser() {
    String id = getQuery().getFirstValue("id");
    return new User(id, "Mark", "Kharitonov");
  }

No need to override the get() methods in this case as the @Get annotation will be automatically detected. 在这种情况下,无需重写get()方法,因为将自动检测@Get批注。

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

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