使用 Restlet 发布绑定上下文路由

issue binding context route using Restlet

我在概念验证中使用 restlet 如下:

    final Component component = new Component();
    component.getServers().add(Protocol.HTTP, 8182);
    final Router router = new Router(component.getContext().createChildContext());
    router.attachDefault(HttpListener.class);
    component.start();

这应该给我 http://localhost:8182/* 的 URL 路径。但是,尝试从此 URL:

获取时,我只收到 404 错误
http://localhost:8182/      -> 404
http://localhost:8182/xyz   -> 404

Restlet 没有将任何请求路由到我的 HttpListener class。

我的 HttpListener class:

public class HttpListener extends ServerResource {

    public static void main(String[] args) throws Exception {
        final Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);
        final Router router = new Router(component.getContext().createChildContext());
        router.attachDefault(HttpListener.class);
        component.start();
    }

    @Get()
    public Representation getDBName() {
        String body = "hello, world";
        Representation representation = new StringRepresentation(body, MediaType.APPLICATION_JSON);
        representation.setCharacterSet(CharacterSet.UTF_8);
        return representation;
    }
}

我必须按如下方式附加路线:

public static void main(String[] args) throws Exception {

    final Router router = new Router();
    router.attachDefault(HttpListener.class);

    Application myApp = new Application() {
        @Override
        public org.restlet.Restlet createInboundRoot() {
            router.setContext(getContext());
            return router;
        };
    };
    Component component = new Component();
    component.getDefaultHost().attach("/", myApp);

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

感谢 的回答。