我们正在使用 Grizzly 和 Jersey 构建 Web 服务,并通过进行 Web 搜索来学习。我们注意到启动 grizzly 网络服务器的两种不同方式,但不知道一种方式是否比另一种更好。有人可以帮忙吗?
1 - 使用 GrizzlyWebContainerFactory:
public static void main(String[] args) throws IOException {
Map initParams = new HashMap();
initParams.put( "com.sun.jersey.config.property.packages", "net.gilstraps.server" );
SelectorThread selector = GrizzlyWebContainerFactory
.create( "http://localhost:9998/", initParams ); //noinspection ResultOfMethodCallIgnored
System.in.read();
threadSelector.stopEndpoint();
System.exit(0);
}
}
2 - 使用 GrizzlyWebServer
GrizzlyWebServer gws = new GrizzlyWebServer(8080, "/var/www");
// Jersey web resources
ServletAdapter jerseyAdapter = new ServletAdapter();
jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages",
"com.example");
jerseyAdapter.setContextPath("/jersey");
jerseyAdapter.setServletInstance(new ServletContainer());
// Another non-Jersey servlet
ServletAdapter simpleServletAdapter = new ServletAdapter();
simpleServletAdapter.setContextPath("/simple");
simpleServletAdapter.setServletInstance(new SimpleServlet());
// register all above defined adapters
gws.addGrizzlyAdapter(jerseyAdapter, new String[] {"/jersey"});
gws.addGrizzlyAdapter(simpleServletAdapter, new String[] {"/simple"});
// let Grizzly run
gws.start();
那么哪种方式更好呢?
谢谢——亚历克斯