Jetty:如何嵌套HandlerWrapper,HandlerList和ContextHandlerCollection,以及ContextHandler [英] Jetty: How to nest HandlerWrapper, HandlerList, and ContextHandlerCollection, and ContextHandler
问题描述
我正在尝试在Jetty上构建一个api服务器。
I'm trying to build an api server on Jetty.
我希望在看起来像/ apis / api1 / endpoint,/ apis的路由上有多个api / api2 / endpoint,/ apis / api3 / endpoint等等
I want to have multiple apis on routes that look like /apis/api1/endpoint, /apis/api2/endpoint, /apis/api3/endpoint, etc
基本上我有一个HandlerWrapper,它包含一个ContextHandlerCollections的HandlerList,实质上就是这样:
Essentially I have a HandlerWrapper, that contains a HandlerList of ContextHandlerCollections that in essence just does:
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
}
现在我尝试这样做:
curl localhost:port/apis/api1/endpoint
我找不到404,但我在日志中看到声明这是一个api请求。
I get a 404 not found but I see in the logs the statement "This is an api request".
任何提示?
我基本上每个api1,api2等都需要一个ContextHandlerCollection。而ContextHandlerCollection应该由一组特定于端点的处理程序组成。
I basically want one ContextHandlerCollection for each api1, api2 etc. And the ContextHandlerCollection should be composed of a set of endpoint-specific handlers to choose from.
我缺少什么?
干杯,
推荐答案
处理程序
- 处理请求的基本形式,它不是请求处理的终点,除非您调用请求。 setHandled(true)
Handler
- the base form of handling a request, its not a terminal point for the request processing unless you call request.setHandled(true)
HandlerWrapper
- 一个可以执行某些处理并决定的处理程序如果它应该将请求移交给嵌套(包装)处理程序。
HandlerWrapper
- a handler that can perform some processing and decide if it should hand off the request to a nested (wrapped) handler.
HandlerCollection
- a处理程序的集合,遵循有关执行顺序的标准java集合规则。执行集合中的每个处理程序,直到其中一个调用 request.setHandled(true)
HandlerCollection
- a collection of handlers, following the standard java collection rules regarding execution order. Each handler in the collection is executed until one of them calls request.setHandled(true)
HandlerList
- 一个专门的HandlerCollection,遵循java.util.List执行子处理程序的顺序
HandlerList
- a specialized HandlerCollection that follows java.util.List ordering of execution of child Handlers
ContextHandler
- 一个专门的HandlerWrapper,只有在请求上下文路径和虚拟主机匹配时才执行其包装的Handler。
ContextHandler
- a specialized HandlerWrapper that only executes its wrapped Handler if the request context-path and virtual hosts matches.
ContextHandlerCollection
- 一个 HashMap> ContextHandler
,它只会执行那些与请求上下文路径(和虚拟主机)匹配的子处理程序(在集合中) )
ContextHandlerCollection
- a HashMap of ContextHandler
that will only execute those child handlers (in the collection) that has a match to the request context-path (and virtual hosts)
这篇关于Jetty:如何嵌套HandlerWrapper,HandlerList和ContextHandlerCollection,以及ContextHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!