3

Restlet 2.1.0、Java SE バージョンでプロトタイピングを行っていますが、ServerResource クラスを URL にマッピングする際に問題が発生しています。Router.attach メソッドを使用してかなりの数のバリエーションを試しましたが、何も機能しませんでした。

私の現在のコードは次のようになります。

/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    final Router router = new Router();
    router.attach("/hello", FirstServerResource.class);
    router.attach("/json", Json.class);

    Application myApp = new Application() {
        @Override
        public org.restlet.Restlet createInboundRoot() {
            router.setContext(getContext());
            return router;
        };
    };

    new Server(Protocol.HTTP, 8182, myApp).start();
}

参照するhttp://localhost:8182/helloと、テンプレートが正しく一致しません。http://localhost:8182/helloソース コードをデバッグすると、一致ロジックが要求されたリソースを単に ではなく と見なすことがわかります/hello。これが発生する Restlet コードは次のとおりです。

// HttpInboundRequest.java
// Set the resource reference
if (resourceUri != null) {
    setResourceRef(new Reference(getHostRef(), resourceUri));

    if (getResourceRef().isRelative()) {
        // Take care of the "/" between the host part and the segments.
        if (!resourceUri.startsWith("/")) {
            setResourceRef(new Reference(getHostRef().toString() + "/"
                    + resourceUri));
        } else {
            setResourceRef(new Reference(getHostRef().toString()
                    + resourceUri));
        }
    }

    setOriginalRef(getResourceRef().getTargetRef());
}

上記のコードでは、 Resource がrelativeと見なされるため、要求され/helloたリソースが完全な URL に変更されます。ここで明らかな何かが欠けていますが、完全に困惑しています。

4

1 に答える 1

4

最後に、ロギング (FINE) をオンにして解決策を見つけました。次のログ メッセージが表示されました。

デフォルトでは、アプリケーションの発信ルートが呼び出しを適切に処理できるように、アプリケーションを親コンポーネントにアタッチする必要があります。

私はそれが何を意味するのか完全には理解していません (ドキュメントを最初から最後まで読まなければならないのでしょうか?)。アプリケーションをVirtualHostに接続すると、問題が修正されました。

public static void main(String[] args) throws Exception {   
    final Router router = new Router();
    router.attach("/hello", FirstServerResource.class);
    router.attach("/json", Json.class);
    router.attachDefault(Default.class);

    Application myApp = new Application() {
        @Override
        public org.restlet.Restlet createInboundRoot() {
            router.setContext(getContext());                
            return router;
        };
    };


    Component component = new Component();
    component.getDefaultHost().attach("/test", myApp);

    new Server(Protocol.HTTP, 8182, component).start();
}
于 2012-10-31T19:09:01.850 に答える