我正在使用 Jetty 9.0.6 将我的 Jersey Web 应用程序从 Jersey 1.17.1 迁移到 2.3.1。使用 Jersey 1.17.1,资源将在启动期间加载。但是,几乎相同的代码(如下)仅在请求进入时启动。但我想确保它在启动期间加载(即,调用带注释的资源类的构造函数)。知道有什么问题吗?
public static void startServer(){
org.eclipse.jetty.server.Server server = new Server();
org.eclipse.jetty.servlet.ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
org.glassfish.jersey.servlet.ServletContainer servletContainer = new ServletContainer();
org.eclipse.jetty.servlet.ServletHolder servletHolder = new ServletHolder(servletContainer);
servletHolder.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "mypackage");
servletHolder.setInitOrder(0); // this does not seem to work
context.addServlet(servletHolder, "/*");
server.start();
}
大致上:
package mypackage;
@Singleton
@Path("/foo")
public class MyResource {
public MyResource(){
// I want this to be called when I start the server
}
@Path("bar")
public Response receivePayload(@Context HttpServletRequest request, String payloadString){
// do some stuff
return response;
}
}