繁体   English   中英

使用Jetty启动Jersey应用程序

[英]Start Jersey application with Jetty

我在Ubuntu服务器VCS中安装了jetty 9服务器。 Java Rest项目的结构:

/ opt / jetty / webapps /后端/

  • 建立
  • conf
  • LIB
  • 日志
  • src / com / example / package / backend
    • 资源
      • CategoryResource.java
    • BackendServer.java
  • 网络
    • 网络信息
      • web.xml

BackendServer.java

import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.net.httpserver.HttpServer;

public class BackendServer {

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServerFactory.create("http://localhost:" + "8080" + "/" + "backend/rest", new PackagesResourceConfig("com.droidbrew.androcommerce.backend.resources"));
        DbManager.getInstance();
        server.start();

        System.out.println("Server running");
        System.out.println("Hit return to stop...");
        System.in.read();
        System.out.println("Stopping server");
        server.stop(0);
        System.out.println("Server stopped");
    }
}

CategoryResource.java

Path("/category")
public class CategoryResource {

    @GET
    @Path("/get_all_categories")
    @Produces({MediaType.APPLICATION_JSON})
    public String getAllCategories() throws SQLException {
        List<Category> categoryList = DbManager.getInstance().getBackendCategoryManager().getAllCategories();
        Gson gson = new Gson();
        return gson.toJson(categoryList);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Jersey</display-name>
  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
      <servlet-name>Jersey REST Service</servlet-name>
      <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
      <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name>
          <param-value>com.droidbrew.androcommerce.backend.resources</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>Jersey REST Service</servlet-name>
      <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

/opt/jetty/webapps/backend.xml

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Set name="configurationClasses">
            <Array type="java.lang.String">
              <Item>org.mortbay.jetty.webapp.WebInfConfiguration</Item>
              <Item>org.mortbay.jetty.plus.webapp.EnvConfiguration</Item>
              <Item>org.mortbay.jetty.plus.webapp.Configuration</Item>
              <Item>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</Item>
              <Item>org.mortbay.jetty.webapp.TagLibConfiguration</Item>
            </Array>
    </Set>
    <Set name="contextPath">/</Set>
    <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/backend</Set>

    <New id="development" class="org.mortbay.jetty.plus.naming.Resource">
            <Arg>jdbc/development</Arg>
            <Arg>
                    <New class="org.postgresql.ds.PGConnectionPoolDataSource">
                            <Set name="User">user</Set>
                            <Set name="Password">1111</Set>
                            <Set name="DatabaseName">development</Set>
                            <Set name="ServerName">localhost</Set>
                            <Set name="PortNumber">5432</Set>
                    </New>
            </Arg>
    </New>
</Configure>

但是在我启动service jetty start并尝试连接ip:8085/backend/rest/category/get_all_categories我得到了:

HTTP错误:404

访问 / backend / rest / category / get_all_categories时出现问题。 原因

未找到

java -DDEBUG -jar start.jar之后的UPDATE日志

sudo java -DDEBUG -jar start.jar
System Property [DEBUG] has been deprecated! (Use org.eclipse.jetty.LEVEL=DEBUG instead)
2014-10-06 13:09:09.522:INFO::main: Logging initialized @1090ms
ShutdownMonitor not in use (port < 0): -1
2014-10-06 13:09:10.047:INFO:oejs.Server:main: jetty-9.2.3.v20140905
2014-10-06 13:09:10.073:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/opt/jetty/webapps/] at interval 1
2014-10-06 13:09:10.096:WARN:oejd.DeploymentManager:main: Unable to reach node goal: started
java.lang.ClassNotFoundException: org.mortbay.jetty.webapp.WebAppContext
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at org.eclipse.jetty.util.Loader.loadClass(Loader.java:86)
    at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.nodeClass(XmlConfiguration.java:364)
    at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:304)
    at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:262)
    at org.eclipse.jetty.deploy.providers.WebAppProvider.createContextHandler(WebAppProvider.java:291)
    at org.eclipse.jetty.deploy.App.getContextHandler(App.java:101)
    at org.eclipse.jetty.deploy.bindings.StandardDeployer.processBinding(StandardDeployer.java:36)
    at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
    at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:498)
    at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:146)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
    at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:609)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:528)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:391)
    at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:313)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:150)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:560)
    at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:235)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.server.Server.start(Server.java:387)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.Server.doStart(Server.java:354)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1255)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1174)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:310)
    at org.eclipse.jetty.start.Main.start(Main.java:786)
    at org.eclipse.jetty.start.Main.main(Main.java:111)
2014-10-06 13:09:10.111:WARN:oejuc.AbstractLifeCycle:main: FAILED ServerConnector@1d402894{HTTP/1.1}{0.0.0.0:8080}: java.net.BindException: Address already in use
java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:444)
    at sun.nio.ch.Net.bind(Net.java:436)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:320)
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.server.Server.doStart(Server.java:366)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1255)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1174)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:310)
    at org.eclipse.jetty.start.Main.start(Main.java:786)
    at org.eclipse.jetty.start.Main.main(Main.java:111)
2014-10-06 13:09:10.114:WARN:oejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.server.Server@5f281b8c: java.net.BindException: Address already in use
java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:444)
    at sun.nio.ch.Net.bind(Net.java:436)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:320)
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.server.Server.doStart(Server.java:366)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1255)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1174)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:310)
    at org.eclipse.jetty.start.Main.start(Main.java:786)
    at org.eclipse.jetty.start.Main.main(Main.java:111)
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:310)
    at org.eclipse.jetty.start.Main.start(Main.java:786)
    at org.eclipse.jetty.start.Main.main(Main.java:111)
Caused by: java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:444)
    at sun.nio.ch.Net.bind(Net.java:436)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:320)
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.server.Server.doStart(Server.java:366)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1255)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1174)
    ... 7 more

您的XML文件引用了旧版本的Jetty。

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

org.mortbay.jetty名称空间适用于Jetty 6或Jetty的旧版本(所有这些已经被EOLd多年了)。

对于Jetty 9,整个XML文档都是错误的,从DOCTYPE到类引用,甚至是结构。

<SystemProperty>元素引用了在jetty嵌入式中不使用的"jetty.home"变量(仅在其中用于jetty分发,也称为start.jar)。 jetty-http-spi.jar甚至没有设置或使用此属性。

Jetty jetty-deploy.jar提供了Jetty Context XML Deployable的使用功能,但是com.sun.net.httpserver.HttpServer概念不支持这种可部署性,它是独立的可部署的,还是内部的WEB-INF/jetty-web.xml配置。

您尝试设置的整个后端服务器大战只能在您启动的com.sun.net.httpserver.HttpServer (基本概念不支持该概念)

不幸的是,当您使用com.sun.net.httpserver.HttpServer技术时,几乎没有可用的故障排除方法。 几乎仅限于日志输出。

日志记录输出显示什么?

更新:2014年10月7日:

关于日志输出,让我们看看。

您将使用Jetty 9.2.3发行版通过命令行启动码头。

该行为与BackendServercom.sun.net.httpserver.HttpServer使用完全无关。

这将导致2个不同的服务器。

  1. 独立的Jetty 9.2.3分发服务器
  2. com.sun.net.httpserver.HttpServer服务器

这些日志中基本上显示2个错误。

首先是...

java.lang.ClassNotFoundException: org.mortbay.jetty.webapp.WebAppContext

那是因为您将Jetty 6技术用于Jetty9。该类(实际上是包名称空间)在Jetty 9中不存在。

该类(在Jetty 9中)将称为org.eclipse.jetty.webapp.WebAppContext ,有关详细信息,请参阅Eclipse Jetty文档网站中的配置特定的Web应用程序部署

自Jetty 6和Jetty 9以来,您将不得不分析和调整示例XML文件中的大多数内容,以适应近200个发行版的实际情况。Jetty6和Jetty 9之间花费了太多的时间来列出已更改的内容。 这等同于列出福特Model 18和特斯拉Model S之间的差异(这将是一个庞大的清单,从本质上讲意味着“一切”,无济于事)

另一个错误...

FAILED ServerConnector@1d402894{HTTP/1.1}{0.0.0.0:8080}: 
java.net.BindException: Address already in use    

表示该服务器无法绑定到端口8080,因为其他服务器已经存在。

这很有意义,因为BackendServer也在端口8080上(根据您的示例代码)。

不要尝试将embed-jetty和com.sun.net.httpserver.HttpServer混合使用,只会勉强地使com.sun.net.httpserver.HttpServer发挥作用。

考虑改用直接嵌入式码头+球衣servlet。

在线和StackOverflow中都有很多这种技术的示例。

这是我发现的第一个示例代码,该示例代码显示了如何为球衣ServletContainer设置ServletHolder以及一些相关的初始化参数来对其进行配置。 配置Jersey + Jetty + JSP

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM