作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这里似乎有很多问题,但没有一个对我有帮助....试图将单个 Java 类作为起点,运行带有 Jersey 的嵌入式 Jetty 以提供网页和 JSON 接口(interface)......但是即使是第一步也无法提供多个页面。
这很好用
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());
但添加其他内容失败。如何提供提供不同内容类型的多个页面?在单个 EntryPoint 类中添加内容的唯一解决方案是什么?
提前感谢任何提示需要改变什么
public class App {
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(8080);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());
ServletHolder helloWorldServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
helloWorldServlet.setInitOrder(1);
helloWorldServlet.setInitParameter("jersey.config.server.provider.classnames", HelloWorldService.class.getCanonicalName());
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e){
System.out.println("Failed running jettyServer with " + e.getMessage());
} finally {
jettyServer.destroy();
}
}
最佳答案
居然找到了解决办法。缺少关键信息是您简单地需要每个正确的处理程序,将它们放在处理程序列表中,瞧,您就在那里......
主要是在找到它之后取自 jetty 文档
public class JettyServer
{
public static void main(String[] args) throws Exception
{
// Create a basic Jetty server object that will listen on port 8080. Note that if you set this to port 0
// then a randomly available port will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
Server server = new Server(8080);
// Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
// a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
ResourceHandler resource_handler = new ResourceHandler();
// Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
// In this example it is the current directory but it can be configured to anything that the jvm has access to.
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "./html/index.html" });
resource_handler.setResourceBase(".");
//Jersey ServletContextHandler
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
ServletHolder jerseyServlet = servletContextHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/api/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", EntryPoint.class.getCanonicalName());
// Add the ResourceHandler to the server.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler, new DefaultHandler() });
server.setHandler(handlers);
// Start things up! By using the server.join() the server thread will join with the current thread.
// See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
server.start();
server.join();
}
对我有帮助...
关于java - 带有 jerseyServlets 的嵌入式 Jetty 中的错误 "Multiple servlets map to path:/*: ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923810/
我正在使用 JerseyServletContainer 机制来部署我的 REST Web 服务。 web.xml 中的配置如下所示: MyServlet org.glassfish
这里似乎有很多问题,但没有一个对我有帮助....试图将单个 Java 类作为起点,运行带有 Jersey 的嵌入式 Jetty 以提供网页和 JSON 接口(interface)......但是即使是
我是一名优秀的程序员,十分优秀!