Restlet - 使用Router附加Resource类时出现问题 [英] Restlet - trouble attaching Resource class with Router

查看:121
本文介绍了Restlet - 使用Router附加Resource类时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Restlet 2.1.0,Java SE版本进行原型设计,我无法将ServerResource类映射到URL。我使用Router.attach方法尝试了很多变种,但没有任何效果。

Prototyping with Restlet 2.1.0, Java SE version, I am having trouble mapping ServerResource classes to urls. I've tried quite a few variations using the Router.attach method, but nothing has worked.

我目前的代码如下所示:

My current code looks like this:

/**
 * @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 而不是 /你好。发生这种情况的Restlet代码在这里:

When I browse to http://localhost:8182/hello it doesn't do the template match correctly. Debugging through the source code, I see what happens is that the match logic sees the requested resource as http://localhost:8182/hello instead of just /hello. The Restlet code where this happens is here:

// 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());
}

在上面的代码中,它将资源视为 relative ,因此将请求的资源从 / hello 更改为完整的URL。我在这里遗漏了一些明显的东西,但我完全被难倒了。

In the code above, it sees the Resource as relative, and therefore changes the requested resource from /hello to the full url. I'm missing something obvious here, but I'm totally stumped.

推荐答案

最后通过打开日志记录找到解决方案(精细)。我看到了这条日志消息:

Finally found the solution by turning on the logging (FINE). I saw this log message:


默认情况下,应用程序应以
顺序附加到父组件,以便让应用程序出站root句柄调用正确。

By default, an application should be attached to a parent component in order to let application's outbound root handle calls properly.

我不完全理解它的意思(也许我必须阅读文档开始完成?)。将应用程序附加到 VirtualHost 修复了问题:

I don't totally understand what it meant (maybe I have to read docs start to finish?). Attaching the application to a VirtualHost fixed the problem:

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();
}

这篇关于Restlet - 使用Router附加Resource类时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆