先从码头泽西应用(Start Jersey application with Jetty)

2019-11-03 09:06发布

我安装码头9服务器在我的Ubuntu服务器VCS。 其余的Java项目的结构:

/选择/码头/ webapps /下后端/

  • 建立
  • CONF
  • LIB
  • 日志
  • SRC / COM /示例/包/后端
    • 资源
      • CategoryResource.java
    • BackendServer.java
  • 卷筒纸
    • WEB-INF
      • 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

问题访问 /后端/ REST /分类/ get_all_categories。 原因

未找到

更新日志java -DDEBUG -jar start.jar

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

Answer 1:

XML文件所引用的是旧版本的Jetty。

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

org.mortbay.jetty命名空间是码头6或以上版本的码头,(所有这些已经被现在EOLD年)。

这整个XML文档是错误的码头9,从DOCTYPE,对类的引用,甚至结构。

所述<SystemProperty>元素引用一个"jetty.home"未在嵌入式码头使用(其只存在于码头分布,又名start.jar)变量。 该jetty-http-spi.jar甚至不设置或使用该属性。

使用码头语境XML部署的是,特征jetty-deploy.jar提供,但是,那种展开的不被支持的com.sun.net.httpserver.HttpServer概念,或者作为一个独立的可展开的,或内部WEB-INF/jetty-web.xml配置。

你正试图建立的那一个后端服务器的战争只是不能在com.sun.net.httpserver.HttpServer你开始(在最基本的层面由概念不支持)

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

什么是日志输出显示?

更新日期:2014年10月7日:

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

你是通过命令行启动码头,用码头9.2.3分布。

该行为是完全无关的BackendServer使用com.sun.net.httpserver.HttpServer

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

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

你有本质上2个错误这些日志中显示。

首先是...

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

那是因为你正在使用码头6个技术码头9该类(实际上包命名空间)在码头9不存在。

那类(在码头9)将被称为org.eclipse.jetty.webapp.WebAppContext ,请参阅配置特定的Web应用程序部署在Eclipse码头文档网站了解详情。

你必须分析和调整的大多数事情在您的示例XML文件,以适应近200释放的现实,因为码头6和Jetty 9.太多时间已经过去了6码头和Jetty 9之间,列出了什么变化。 这将相当于上市福特模型18和特斯拉S型之间的差异(这将是一个巨大的名单,将主要的意思是“一切”,并关闭无用的工作)

其他错误...

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

意味着该服务器不能绑定到8080端口,其他一些服务器已经存在。

这是有道理的,因为你的BackendServer也是在端口8080(根据您的示例代码)。

不要尝试混合嵌入式码头和com.sun.net.httpserver.HttpServer ,你只会撕裂你的头发,试图迫使com.sun.net.httpserver.HttpServer去做你想要什么。

考虑使用直嵌入式码头+球衣的servlet来代替。

有很多的这种技术的例子在网上和StackOverflow上。

这是我发现的第一个显示了如何设置示例代码ServletHolder的球衣ServletContainer一些相关的初始化参数一起进行配置。 配置新泽西+码头+ JSP



文章来源: Start Jersey application with Jetty