我在我的Ubuntu服务器VCS中安装了jetty 9服务器。 java rest项目的结构:
/ opt / jetty / webapps / backend /
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
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
答案 0 :(得分:0)
您的XML文件引用了旧版本的Jetty。
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
org.mortbay.jetty
命名空间适用于Jetty 6或更早版本的Jetty(所有这些版本已经多年来一直是EOLd)。
整个XML文档对于Jetty 9来说是错误的,从DOCTYPE到类引用,甚至是结构。
<SystemProperty>
元素引用了一个未在jetty嵌入中使用的"jetty.home"
变量(它仅用于jetty发布,即start.jar)。 jetty-http-spi.jar
甚至没有设置或使用此属性。
使用Jetty Context XML Deployable是jetty-deploy.jar
提供的一项功能,但com.sun.net.httpserver.HttpServer
概念不支持这种可部署,可以是独立的可部署的,也可以是内部{ {1}}配置。
您尝试设置的整个后端服务器战争不能在您开始的WEB-INF/jetty-web.xml
上(基本级别不支持该概念)
不幸的是,使用com.sun.net.httpserver.HttpServer
技术时几乎没有可用的故障排除方法。几乎只限于记录输出。
日志输出显示什么?
更新:2014年10月7日:
关于日志记录输出,请参阅。
您正在使用Jetty 9.2.3发行版通过命令行启动jetty。
该行为与您com.sun.net.httpserver.HttpServer
使用BackendServer
完全无关。
这将产生2个不同的服务器。
com.sun.net.httpserver.HttpServer
服务器这些日志中显示的错误实际上有2个。
首先是......
com.sun.net.httpserver.HttpServer
那是因为你在Jetty 9中使用了Jetty 6技术。该类(实际上是包名称空间)在Jetty 9中不存在。
该类(在Jetty 9中)将被称为org.eclipse.jetty.webapp.WebAppContext
,有关详细信息,请参阅Eclipse Jetty文档网站中的Configuring a Specific Web App Deployment。
您必须分析和调整示例XML文件中的大多数内容,以适应自Jetty 6和Jetty 9以来近200个版本的实际情况。在Jetty 6和Jetty 9之间已经过了太多时间来列出已更改的内容。这相当于列出了福特18型和特斯拉型号S之间的差异(这将是一个巨大的列表,基本上意味着“一切”并且无用工作)
另一个错误......
java.lang.ClassNotFoundException: org.mortbay.jetty.webapp.WebAppContext
表示此服务器无法绑定到端口8080,因为其他服务器已经存在。
这是有道理的,因为您的FAILED ServerConnector@1d402894{HTTP/1.1}{0.0.0.0:8080}:
java.net.BindException: Address already in use
也在端口8080上(根据您的示例代码)。
不要尝试混合嵌入式码头和BackendServer
,你只会撕掉你的头发,试图强迫com.sun.net.httpserver.HttpServer
做你想做的事。
请考虑使用直嵌式Jetty + jersey servlet。
在线和StackOverflow中有很多这种技术的例子。
这是我发现的第一个示例代码,其中显示了如何为jersey com.sun.net.httpserver.HttpServer
设置ServletHolder
以及一些相关的init参数来配置它。 Configuring Jersey + Jetty + JSP