我在restlet中遇到了一个奇怪的路由问题。我正在使用android。
此课程extends Router
这是我的路由代码:
this.attach("/contacts", contactListRestlet); // To list all contacts
this.attach("/contacts/{uid}", contactRestlet); // To show details about a specific contact
this.attach("/contacts/similar", similarRestlet, Template.MODE_EQUALS);
this.attach("/contact/photo/{uid}", photoRestlet); // {uid} must correspond to a valid raw contact id
this.attach("/contact/photo/{uid}/thumbnail", photoThumbnailRestlet);
this.attach("/import/file", fileImportRestlet);
this.attach("/echo", echoRestlet);
this.attach("/echo/file", echoFileRestlet);
this.attach("/import/vcard", importVcard);
this.attach("/logout", logoutRestlet);
final String ROOT_URI = "file:///android_asset";
Application application = new Application() {
@Override
public Restlet createInboundRoot() {
//ForAssets
DirectoryForAssets d = new DirectoryForAssets(getContext(), ROOT_URI);
d.setTargetClass(DirectoryResourceExtension.class);
d.setAndroidContext(context);
return d;
}
};
this.attach("/editor", application, Template.MODE_STARTS_WITH);
这样一来,如果我点击ip:port/contacts/similar
,我总会被重定向到/contacts restlet
(contactRestlet
)而不是/contacts/similar restlet
(similarRestlet
),正如我所期待的那样。
但如果我把它改为:
this.attach("/contacts/similar", similarRestlet);
this.attach("/contacts", contactListRestlet); // To list all contacts
this.attach("/contacts/{uid}", contactRestlet); // To show details about a specific contact
this.attach("/contact/photo/{uid}", photoRestlet); // {uid} must correspond to a valid raw contact id
this.attach("/contact/photo/{uid}/thumbnail", photoThumbnailRestlet);
this.attach("/import/file", fileImportRestlet);
this.attach("/echo", echoRestlet);
this.attach("/echo/file", echoFileRestlet);
this.attach("/import/vcard", importVcard);
this.attach("/logout", logoutRestlet);
final String ROOT_URI = "file:///android_asset";
Application application = new Application() {
@Override
public Restlet createInboundRoot() {
//ForAssets
DirectoryForAssets d = new DirectoryForAssets(getContext(), ROOT_URI);
d.setTargetClass(DirectoryResourceExtension.class);
d.setAndroidContext(context);
return d;
}
};
this.attach("/editor", application, Template.MODE_STARTS_WITH);
它确实有效! (唯一的区别是similarRestlet优先于contactRestlet)。
任何人都可以解释我为什么会这样做吗?我做错了什么?
答案 0 :(得分:3)
默认情况下,路由器匹配第一个可用匹配,其他匹配模式包括最佳匹配。这可以通过更改setDefaultMatchingMode(int)
来调整。有关其他信息,请参阅restlet JavaDoc。目前位于http://restlet.com/technical-resources/restlet-framework/javadocs/2.3/jse/api/org/restlet/routing/Router.html
答案 1 :(得分:1)
"/contacts/{uid}"
"/contacts/similar"
如果您有上述序列,它将使用{uid}并填写“类似”,这是因为它无法区分两者,并使用“类似”作为“uid”。
所以当你这样说的时候:
"/contacts/similar"
"/contacts/{uid}"
它首先捕获“相似”,如果它不是“相似”,它将使用该部分作为uid。