【问题标题】:issue binding context route using Restlet使用 Restlet 发出绑定上下文路由
【发布时间】:2015-01-25 09:45:53
【问题描述】:

我在概念验证中使用 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 类。

我的 HttpListener 类:

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

【问题讨论】:

    标签: restlet restlet-2.0


    【解决方案1】:

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

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

    感谢https://stackoverflow.com/a/13165900/1033422 的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多