Saturday, 15 January 2011

java - Start Jersey application with Jetty -



java - Start Jersey application with Jetty -

i install jetty 9 server in ubuntu server vcs. construction of java rest project:

/opt/jetty/webapps/backend /

build conf lib libs logs src/com/example/package/backend resources categoryresource.java backendserver.java web 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 homecoming 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(); homecoming 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>

but after start service jetty start , seek connect ip:8085/backend/rest/category/get_all_categories get:

http error: 404

problem accessing /backend/rest/category/get_all_categories. reason:

not found

update logs after java -ddebug -jar start.jar

sudo java -ddebug -jar start.jar scheme 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 utilize (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/] @ interval 1 2014-10-06 13:09:10.096:warn:oejd.deploymentmanager:main: unable reach node goal: started java.lang.classnotfoundexception: org.mortbay.jetty.webapp.webappcontext @ java.net.urlclassloader$1.run(urlclassloader.java:366) @ java.net.urlclassloader$1.run(urlclassloader.java:355) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(urlclassloader.java:354) @ java.lang.classloader.loadclass(classloader.java:425) @ java.lang.classloader.loadclass(classloader.java:358) @ org.eclipse.jetty.util.loader.loadclass(loader.java:86) @ org.eclipse.jetty.xml.xmlconfiguration$jettyxmlconfiguration.nodeclass(xmlconfiguration.java:364) @ org.eclipse.jetty.xml.xmlconfiguration$jettyxmlconfiguration.configure(xmlconfiguration.java:304) @ org.eclipse.jetty.xml.xmlconfiguration.configure(xmlconfiguration.java:262) @ org.eclipse.jetty.deploy.providers.webappprovider.createcontexthandler(webappprovider.java:291) @ org.eclipse.jetty.deploy.app.getcontexthandler(app.java:101) @ org.eclipse.jetty.deploy.bindings.standarddeployer.processbinding(standarddeployer.java:36) @ org.eclipse.jetty.deploy.applifecycle.runbindings(applifecycle.java:186) @ org.eclipse.jetty.deploy.deploymentmanager.requestappgoal(deploymentmanager.java:498) @ org.eclipse.jetty.deploy.deploymentmanager.addapp(deploymentmanager.java:146) @ org.eclipse.jetty.deploy.providers.scanningappprovider.fileadded(scanningappprovider.java:180) @ org.eclipse.jetty.deploy.providers.scanningappprovider$1.fileadded(scanningappprovider.java:64) @ org.eclipse.jetty.util.scanner.reportaddition(scanner.java:609) @ org.eclipse.jetty.util.scanner.reportdifferences(scanner.java:528) @ org.eclipse.jetty.util.scanner.scan(scanner.java:391) @ org.eclipse.jetty.util.scanner.dostart(scanner.java:313) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.deploy.providers.scanningappprovider.dostart(scanningappprovider.java:150) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.deploy.deploymentmanager.startappprovider(deploymentmanager.java:560) @ org.eclipse.jetty.deploy.deploymentmanager.dostart(deploymentmanager.java:235) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.util.component.containerlifecycle.start(containerlifecycle.java:132) @ org.eclipse.jetty.server.server.start(server.java:387) @ org.eclipse.jetty.util.component.containerlifecycle.dostart(containerlifecycle.java:114) @ org.eclipse.jetty.server.handler.abstracthandler.dostart(abstracthandler.java:61) @ org.eclipse.jetty.server.server.dostart(server.java:354) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.xml.xmlconfiguration$1.run(xmlconfiguration.java:1255) @ java.security.accesscontroller.doprivileged(native method) @ org.eclipse.jetty.xml.xmlconfiguration.main(xmlconfiguration.java:1174) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.eclipse.jetty.start.main.invokemain(main.java:310) @ org.eclipse.jetty.start.main.start(main.java:786) @ 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 in utilize java.net.bindexception: address in utilize @ sun.nio.ch.net.bind0(native method) @ sun.nio.ch.net.bind(net.java:444) @ sun.nio.ch.net.bind(net.java:436) @ sun.nio.ch.serversocketchannelimpl.bind(serversocketchannelimpl.java:214) @ sun.nio.ch.serversocketadaptor.bind(serversocketadaptor.java:74) @ org.eclipse.jetty.server.serverconnector.open(serverconnector.java:320) @ org.eclipse.jetty.server.abstractnetworkconnector.dostart(abstractnetworkconnector.java:80) @ org.eclipse.jetty.server.serverconnector.dostart(serverconnector.java:236) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.server.server.dostart(server.java:366) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.xml.xmlconfiguration$1.run(xmlconfiguration.java:1255) @ java.security.accesscontroller.doprivileged(native method) @ org.eclipse.jetty.xml.xmlconfiguration.main(xmlconfiguration.java:1174) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.eclipse.jetty.start.main.invokemain(main.java:310) @ org.eclipse.jetty.start.main.start(main.java:786) @ 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 in utilize java.net.bindexception: address in utilize @ sun.nio.ch.net.bind0(native method) @ sun.nio.ch.net.bind(net.java:444) @ sun.nio.ch.net.bind(net.java:436) @ sun.nio.ch.serversocketchannelimpl.bind(serversocketchannelimpl.java:214) @ sun.nio.ch.serversocketadaptor.bind(serversocketadaptor.java:74) @ org.eclipse.jetty.server.serverconnector.open(serverconnector.java:320) @ org.eclipse.jetty.server.abstractnetworkconnector.dostart(abstractnetworkconnector.java:80) @ org.eclipse.jetty.server.serverconnector.dostart(serverconnector.java:236) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.server.server.dostart(server.java:366) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.xml.xmlconfiguration$1.run(xmlconfiguration.java:1255) @ java.security.accesscontroller.doprivileged(native method) @ org.eclipse.jetty.xml.xmlconfiguration.main(xmlconfiguration.java:1174) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.eclipse.jetty.start.main.invokemain(main.java:310) @ org.eclipse.jetty.start.main.start(main.java:786) @ org.eclipse.jetty.start.main.main(main.java:111) java.lang.reflect.invocationtargetexception @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.eclipse.jetty.start.main.invokemain(main.java:310) @ org.eclipse.jetty.start.main.start(main.java:786) @ org.eclipse.jetty.start.main.main(main.java:111) caused by: java.net.bindexception: address in utilize @ sun.nio.ch.net.bind0(native method) @ sun.nio.ch.net.bind(net.java:444) @ sun.nio.ch.net.bind(net.java:436) @ sun.nio.ch.serversocketchannelimpl.bind(serversocketchannelimpl.java:214) @ sun.nio.ch.serversocketadaptor.bind(serversocketadaptor.java:74) @ org.eclipse.jetty.server.serverconnector.open(serverconnector.java:320) @ org.eclipse.jetty.server.abstractnetworkconnector.dostart(abstractnetworkconnector.java:80) @ org.eclipse.jetty.server.serverconnector.dostart(serverconnector.java:236) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.server.server.dostart(server.java:366) @ org.eclipse.jetty.util.component.abstractlifecycle.start(abstractlifecycle.java:68) @ org.eclipse.jetty.xml.xmlconfiguration$1.run(xmlconfiguration.java:1255) @ java.security.accesscontroller.doprivileged(native method) @ org.eclipse.jetty.xml.xmlconfiguration.main(xmlconfiguration.java:1174) ... 7 more

your xml files referencing old version of jetty.

<configure class="org.mortbay.jetty.webapp.webappcontext">

the org.mortbay.jetty namespace jetty 6 or older versions of jetty, (all of have been eold years now).

that entire xml document wrong jetty 9, doctype, class references, structure.

the <systemproperty> element references "jetty.home" variable isn't used in jetty embedded (its there jetty distribution, aka start.jar). jetty-http-spi.jar doesn't set or utilize property.

the utilize of jetty context xml deployable feature jetty-deploy.jar provides, however, sort of deployable not supported com.sun.net.httpserver.httpserver concepts, either standalone deployable, or internal web-inf/jetty-web.xml configuration.

that whole backend server war attempting setup cannot on com.sun.net.httpserver.httpserver started (not supported concept @ fundamental level)

unfortunately, there no troubleshooting available when utilize com.sun.net.httpserver.httpserver techniques. pretty much limited logging output.

what logging output show?

update: 2014, oct 7:

about logging output, lets see.

you starting jetty via command line, using jetty 9.2.3 distribution.

that deed unrelated backendserver utilize of com.sun.net.httpserver.httpserver.

this result in 2 different servers.

the standalone jetty 9.2.3 distribution server the com.sun.net.httpserver.httpserver server

you have 2 errors showing in logs.

first ...

class="lang-none prettyprint-override">java.lang.classnotfoundexception: org.mortbay.jetty.webapp.webappcontext

that's because using jetty 6 techniques jetty 9. class (actually bundle namespace) doesn't exist in jetty 9.

that class (in jetty 9) called org.eclipse.jetty.webapp.webappcontext, see configuring specific web app deployment in eclipse jetty documentation website details.

you have analyze , adjust things in illustration xml file fit realities of 200 releases since jetty 6 , jetty 9. far much time has elapsed between jetty 6 , jetty 9 list has changed. equivalent listing differences between ford model 18 , tesla model s (it huge list mean 'everything' , useless work off of)

the other error ...

class="lang-none prettyprint-override">failed serverconnector@1d402894{http/1.1}{0.0.0.0:8080}: java.net.bindexception: address in utilize

means server cannot bind port 8080, other server there.

this makes sense, backendserver on port 8080 (per illustration code).

don't seek mix embedded-jetty , com.sun.net.httpserver.httpserver, tear hair out trying forcefulness com.sun.net.httpserver.httpserver doing want.

consider using straight embedded-jetty + bailiwick of jersey servlet instead.

there's lots of examples of technique online , in stackoverflow.

here's first 1 found had illustration code showing how setup servletholder bailiwick of jersey servletcontainer along relevant init parameters configure it. configuring bailiwick of jersey + jetty + jsp

java jersey jetty ubuntu-12.04

No comments:

Post a Comment