最簡單的解決方案將利用Grizzly嵌入式Servlet支持。
這當然意味着您需要做一些工作來將您當前的HttpHandler
邏輯遷移到Servlet
s - 但這確實不應該太難,因爲HttpHandler
API非常相似。
我會給出一些關於這樣做的高級要點。
HttpServer server = HttpServlet.createSimpleServer(<docroot>, <host>, <port>);
// use "" for <context path> if you want the context path to be/
WebappContext ctx = new WebappContext(<logical name>, <context path>);
// do some Jersey initialization here
// Register the Servlets that were converted from HttpHandlers
ServletRegistration s1 = ctx.addServlet(<servlet name>, <Servlet instance or class name>);
s1.addMapping(<url pattern for s1>);
// Repeat for other Servlets ...
// Now for the authentication Filter ...
FilterRegistration reg = ctx.addFilter(<filter name>, <filter instance or class name>);
// Apply this filter to all requests
reg.addMapping(null, "/*");
// do any other additional initialization work ...
// "Deploy" ctx to the server.
ctx.deploy(server);
// start the server and test ...
注:servlet和過濾器的動態註冊是基於關閉了Servlet 3.0 API,所以如果你想如何處理與Servlet的監聽器,初始化參數等信息,我建議審查的Servlet 3.0的javadoc。注2:Grizzly Servlet實現與Servlet規範不是100%兼容的。它不支持標準的Servlet註釋或部署傳統的Servlet Web應用程序存檔部署。
最後,有(與灰熊2.3.5測試)使用嵌入的Servlet API的here