【问题标题】:Restlet routing - Route being overtaken by another routeRestlet 路由 - 路由被另一条路由取代
【发布时间】:2014-03-11 10:34:58
【问题描述】:

我在 restlet 中遇到了一个奇怪的路由问题。我用的是安卓。

本课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先出现)。

谁能解释我为什么会出现这种行为?我做错了什么?

【问题讨论】:

    标签: restlet restlet-2.0


    【解决方案1】:

    路由器默认匹配第一个可用的匹配,其他匹配模式包括最佳匹配存在。这可以通过更改setDefaultMatchingMode(int) 进行调整,有关更多信息,请参阅restlet JavaDoc。目前在http://restlet.com/technical-resources/restlet-framework/javadocs/2.3/jse/api/org/restlet/routing/Router.html

    【讨论】:

      【解决方案2】:
      "/contacts/{uid}"
      "/contacts/similar"
      

      如果你有上面这个序列,它会使用{uid}并填写“相似”这是因为它无法区分两者,并且使用“相似”作为“uid”。

      所以当你这样说的时候:

      "/contacts/similar"
      "/contacts/{uid}"
      

      它首先捕获“相似”,如果不是“相似”,它将使用该部分作为uid。

      【讨论】:

      • Bert 的答案对于第一个可用的匹配匹配模式是正确的(默认)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2017-12-18
      • 2019-09-22
      • 1970-01-01
      • 2020-11-26
      • 2018-01-06
      • 2012-01-26
      相关资源
      最近更新 更多