简体   繁体   English

在Restlet 2.0中获取请求属性

[英]Get Request Attributes in Restlet 2.0

I'm using Restlet to make a RESTful platform. 我正在使用Restlet创建一个RESTful平台。 I haven't used it before, but I decided to use 2.0 because it is better to start with the latest and greatest technology, right? 我以前从未使用过它,但是我决定使用2.0,因为最好从最新最好的技术开始,对吗?

The key thing I am looking for is the ability to have someone to put in a URL like http://mysite/New%20York/3 and have the service respond with something like [New York,New York,New York] , so I need to pass in request attributes. 我要寻找的关键是能够让某人输入诸如http://mysite/New%20York/3类的URL,并使服务响应诸如[New York,New York,New York] ,因此我需要传递请求属性。 Using this post for Restlet 1.1 (because I can't seem to find any documentation for this on the Restlet site), I wired up my application like so: 使用此帖子进行Restlet 1.1 (因为我似乎在Restlet网站上找不到与此相关的任何文档),因此我将应用程序连接起来如下:

router.attach("{text}/{count}", RepeaterResource.class);

The new way to do this is apparently in the UniformResource#doInit() method, so mine looks like (without error checking): 新的方法显然是在UniformResource#doInit()方法中,所以我的看起来像(没有错误检查):

@Override
public void doInit()
{
    magicText = "" + getRequestAttributes().get("text");
    repeatAmount = Integer.parseInt("" + getRequestAttributes().get("count"));
}

The problem is that the Map<String, Object> returned from getRequestAttributes() is always completely empty! 问题是从getRequestAttributes()返回的Map<String, Object>始终完全为空! This seems rather odd. 这似乎很奇怪。 Am I wiring the routing up wrong? 我的布线错误吗?

Of course, I could just use getQuery() and parse it myself, but that is definitely the wrong way to go about doing this and it seems like there should be an easy way to do this (similar to the way previous versions worked). 当然,我可以只使用getQuery()自己解析它,但这绝对是进行此操作的错误方法,并且似乎应该有一种简便的方法(类似于先前版本的工作方式)。

My problem, is seems, is that router attachments must start with the / character. 我的问题似乎是路由器附件必须以/字符开头。 I should attached like so: 我应该这样附上:

router.attach("/{text}/{count}", RepeaterResource.class);

I can't seem to find this behavior documented and it seems rather odd, but it certainly fixed my issues. 我似乎找不到该行为的记录,这似乎很奇怪,但它确实解决了我的问题。

You can do this with 2.0 the same way as with 1.1. 您可以使用2.0来执行此操作,就像使用1.1一样。

See the tutorial, part 11: http://www.restlet.org/documentation/2.0/tutorial#part11 请参阅第11部分的教程: http : //www.restlet.org/documentation/2.0/tutorial#part11

Restlet is great, BTW. Restlet很棒,顺便说一句。

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

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