Jetty:如何嵌套HandlerWrapper、HandlerList、ContextHandlerCollection、ContextHandler

Abh*_*nan 0 java jetty embedded-jetty

我正在尝试在 Jetty 上构建一个 api 服务器。

我希望在 /apis/api1/endpoint、/apis/api2/endpoint、/apis/api3/endpoint 等路径上有多个 api

本质上我有一个 HandlerWrapper,它包含 ContextHandlerCollections 的 HandlerList,本质上只是做:

public void handle(...) {
    if (uri.startsWith("/apis/")) {
        log.info("This is an api request");
        this.getHandlerList.handle(...)
    } else {
        super.handle()
    }
}

private HandlerList getHandlerList() {
    HandlerList handlerList = new HandlerList();
    ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
    ContextHandler api1 = new ContextHandler("/apis/api1/endpoint");
    api1.setHandler(new Api1Handler());
    contextHandlerCollection.addHandler(api1);
    handlerList.addHandler(contextHandlerCollection);
    return handlerList
}
Run Code Online (Sandbox Code Playgroud)

现在当我尝试这样做时:

curl localhost:port/apis/api1/endpoint
Run Code Online (Sandbox Code Playgroud)

我收到 404 not found,但我在日志中看到语句“这是一个 api 请求”。

有什么提示吗?

我基本上希望每个 api1、api2 等都有一个 ContextHandlerCollection。并且 ContextHandlerCollection 应该由一组特定于端点的处理程序组成以供选择。

我缺少什么?

干杯,

Joa*_*elt 6

Handler- 处理请求的基本形式,它不是请求处理的终点,除非您调用request.setHandled(true)

HandlerWrapper- 一个处理程序,可以执行某些处理并决定是否应将请求传递给嵌套(包装)处理程序。

HandlerCollection- 处理程序的集合,遵循有关执行顺序的标准 java 集合规则。执行集合中的每个处理程序,直到其中一个处理程序调用request.setHandled(true)

HandlerList- 一个专门的 HandlerCollection,遵循子处理程序的 java.util.List 执行顺序

ContextHandler- 一个专门的 HandlerWrapper,仅在请求上下文路径和虚拟主机匹配时才执行其包装的 Handler。

ContextHandlerCollection- 其 HashMapContextHandler将仅执行与请求上下文路径(和虚拟主机)匹配的那些子处理程序(在集合中)