[英]Restlet URL/path pattern mismapping
I'm developing a REST API using Restlet. 我正在使用Restlet开发REST API。
So far everything has been working just fine. 到目前为止,一切都很好。 However, I now encountered an issue with the
Router
mapping of URL to ServerResource
. 但是,我现在遇到URL到
ServerResource
的Router
映射的问题。
I've got the following scenario: 我有以下情况:
/car
returns a list of all cars /car
返回所有汽车的列表 /car/{id}
returns details about the car with id 1 /car/{id}
返回ID为1的汽车的详细信息 /car/advancedsearch?param1=test
should run a search across all cars with some parameters /car/advancedsearch?param1=test
应该使用某些参数对所有汽车进行搜索 The first two calls work without any problems. 前两个调用工作正常。 If I try to hit the third call though, the Restlet
Router
somehow maps it to the second one instead. 如果我尝试打第三个电话,那么Restlet
Router
以某种方式将其映射到第二个电话。 How can I tell Restlet to instead use the third case? 如何告诉Restlet使用第三种情况?
My mapping is defined as follows: 我的映射定义如下:
router.attach("/car", CarListResource.class);
router.attach("/car/{id}", CarResource.class);
router.attach("/car/advancedsearch", CarSearchResource.class);
CarSearchResource
is never invoked, but rather the request ends up in CarResource
. CarSearchResource
永远不会被调用,但是请求最终会在CarResource
结束。
The router's default matching mode is set to Template.MODE_EQUALS
, so that can't be causing it. 路由器的默认匹配模式设置为
Template.MODE_EQUALS
,所以不会导致它。
Does anyone have any further suggestions how I could fix it? 有人对我如何解决有其他建议吗?
Please don't suggest to use /car
with the parameters instead, as there's already another kind of search in place on that level. 请不要建议将
/car
用作参数,因为在该级别上已经存在另一种搜索方式。 Also, I'm not in control of the API structure, so it has to remain as it is. 另外,我不受API结构的控制,因此必须保持原样。
you need to add .setMatchingQuery(true);
您需要添加
.setMatchingQuery(true);
to that rout in order it to recognize that it is with a query at the end of it. 到该溃败,以便识别出它的末尾带有查询。
Router router = (Router) super.createInboundRoot();
TemplateRoute route1 = router.attach("/car/advancedsearch?{query_params}", MyResource.class);
route1.setMatchingQuery(true);
return router;
Mind that this pattern is with the exact specific order that you have determined in the route ie advancedsearch
comes first and query_params
comes after 请注意,此模式具有您在路由中确定的确切特定顺序,即
advancedsearch
首先出现, query_params
之后出现
I was able to solve this by simply reordering the attach
statements: 我能够通过简单地重新排序
attach
语句来解决此问题:
router.attach("/car/advancedsearch", CarSearchResource.class);
router.attach("/car", CarListResource.class);
router.attach("/car/{id}", CarResource.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.