Jersey RESTful deployment on Tomcat 6 - web-services

I am using the following setup to deploy a REST webservice on Tomcat 6 and it works:
<web-app>
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
With the above setup, Tomcat scans WEB-INF/classes and WEB-INF/lib folders for the resources.
I then changed it to the following setup to use Application sub-class:
<web-app>
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>test.MyApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
where test.MyApplication is the sub-class of Application returning the resource class:
public class MyApplication extends Application {
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(MyResource.class);
return s;
}
}
But still the server scans WEB-INF/classes and WEB-INF/lib for the resources.
What am I missing? Does the server still scan WEB-INF when Application subclass is set in the web.xml?

Does the server still scan WEB-INF when Application subclass is set in the web.xml?
No it doesn't. The documentation explains that scanning the path is done if no resource configuration related initialization parameters (like javax.ws.rs.Application) are present.
You should see what's in your server logs. When Jersey initializes it logs what it's doing and for a classpath scan it prints something like:
INFO: Scanning for root resource and provider classes in the Web app resource paths:
/WEB-INF/lib
/WEB-INF/classes
When you provide an Application class it prints this instead:
INFO: Instantiated the Application class test.MyApplication
You should make sure that the proper web.xml file is deployed when the server starts. Maybe you still have the old version deployed.

Related

How to deploy static content on root context with jetty runner (standalone jar)

Update:
I've noticed that if I'don't deploy the WAR file i can do it as Joakim Erdfelt says, i'll investigate what changes I've done to the war that causes the failure at root context.
I know how to deploy static content to any context :
sudo java -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner --port 80 MYAPP.war --path /context1 site/MyStaticWebSite
//will deploy mysite in
//127.0.0.1/context1/index.html
but I want to deploy it in the root
127.0.0.1/index.html
And there's no reference in the docs nor in google.
https://webtide.com/downloads/
If I do :
sudo java -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner --port 80 MYAPP.war --path / site/MyStaticWebSite
I get :
2015-11-11 21:05:27.498:INFO::main: Logging initialized #121ms
2015-11-11 21:05:27.513:INFO:oejr.Runner:main: Runner
2015-11-11 21:05:27.618:INFO:oejs.Server:main: jetty-9.2.13.v20150730
Nov 11, 2015 9:05:34 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.6 2014-02-18 21:52:53...
2015-11-11 21:05:35.323:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext#33833882{/,file:/tmp/jetty-0.0.0.0-80-MYAPP.war-_-any-4618238315169821839.dir/webapp/,AVAILABLE}{file:/home/ubuntu/MYAPP.war}
2015-11-11 21:05:35.744:WARN:oeja.AnnotationConfiguration:main: ServletContainerInitializers: detected. Class hierarchy: empty
2015-11-11 21:05:35.871:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext#200a570f{/,file:site/MyStaticWebSite/,AVAILABLE}{/root/MyStaticWebSite/}
2015-11-11 21:05:35.872:WARN:oejsh.RequestLogHandler:main: !RequestLog
2015-11-11 21:05:35.890:INFO:oejs.ServerConnector:main: Started ServerConnector#2504eefd{HTTP/1.1}{0.0.0.0:80}
2015-11-11 21:05:35.891:INFO:oejs.Server:main: Started #8517ms
And root webpage shows the following:
First, upgrade!
9.1.0.M0 is an unstable/beta milestone build.
Use something a bit more recent and stable (how about 9.2.13.v20150730 of jetty-runner?)
If you want it deployed on the root context, use --path / (not --path /context1)
Update:
Ah, you have 2 webapps, and Jersey in the mix, didn't realize.
WebApp 1: context:/ path:MYAPP.war
WebApp 2: context:/ path:site/MyStaticWebSite
No you can't have both on the same context path, that's not supported by the servlet spec.
This is because every webapp MUST terminate once it is entered.
Scenario:
So since the context path is /, both webapps will match
First one entered will service the request
If a servlet has a matching url-pattern, that servlet is called
If no servlet matches, then the DefaultServlet is used.
DefaultServlet will look for a static resource matching the url-pattern/pathInfo
If no static resource matches, then DefaultServlet produces an error.
That webapp must produce a response. There is no opportunity to skip the webapp and move into the next one.
However, this behavior is further complicated by Jersey, its default configuration takes over all static resource serving itself, never allowing Jetty to even have the opportunity to serve those static resources.
What you'll need to do.
your MYAPP.war must have the static resources.
if you have "external" static resources, those must be served by something in MYAPP.war - either a formal servlet that does it, or something in Jersey. Suggestion is to set that external behavior up at a different url-pattern like /external/* or /static/*, not /*.
I've finally found how I could do it,
Instead of:
sudo java -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner --port 80 MYAPP.war --path / site/MyStaticWebSite
Do:
sudo java -cp jetty-runner-9.2.13.v20150730.jar org.eclipse.jetty.runner.Runner --port 80 --path /MYAPP MYAPP.war --path / site/MyStaticWebSite
MYAPP was deploying itself to /MYAPP context as specified in web.xml but didn't allow to deploy any content in the root context, by spec.
Then the problem was that /MYAPP couldn't be reached, but /MYAPP/MYAPP could, that was because the web.xml was saying to deploy the service into /MYAPP so I had to change it
from:
<servlet-mapping>
<servlet-name>MY_SERVLET_NAME</servlet-name>
<url-pattern>/MYAPP/*</url-pattern>
</servlet-mapping>
to:
<servlet-mapping>
<servlet-name>MY_SERVLET_NAME</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Now it works.

Web Service not accessible on Tomcat 7 - 404 error

Hi Friends
I have created a sample web service using netbeans 7.3. It works fine on localhost when running through tomcat linked with netbeans.
When I deploy it in webapps folder of the tomcat 7 on any standalone machine, it works fine too. BUT when we transfer the code from webapps to public_html folder of online linux server, I get 404 error. please see the file. Online server has tomcat 7 which is linked through with Apache
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd">
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>EduCloudWS</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>EduCloudWS</servlet-name>
<url-pattern>/EduCloudWS</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
sun-jaxws.xml
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint implementation="com.educloud.ws.EduCloudWS" name="EduCloudWS" url-pattern="/EduCloudWS"/>
</endpoints>
I am trying to access by following links, both don't work and give a 404 error
http://domainname.com/EduCloudWS
http://domainname.xom/EduCloudWS/EduCloudWS
Thanks for the help

Tomcat 6.0 Deployment Error

I am having problems deploying my web application, and it has been puzzling me for 3 weeks after continually blaming our hosting company and their server.
First of all, the web application was deploying fine using manager on tomcat 5.
Then I asked to change to tomcat 6, which they did, and now the application does not deploy and gives the now all so familiar SEVERE: Error deploying configuration descriptor XXXX.xml.
BUT in Eclipse I build and run my application on Tomcat 6 which works fine!, but when again when I try and deploy the packaged war on my local server via manager, I get the SEVERE error.
There is something wrong with my deployment descriptor which is sensitive to tomcat but not in eclipse. I read somewhere about case sensitive and I have double checked this.
I have then tried and commented out the context - still does not work, and everything in web.xml apart from the welcome-file-list. Still I get the same error.
I am out of ideas and very confused.
Below I have posted web.xml and context.xml (for confidentiality reasons I have to XXXX out the names).
<?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>XXXX</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>restSdkService</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.XXXX.core.TestWebApp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>restSdkService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/XXXX</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/emotifi" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="XXXX" password="XXXX"
driverClassName="com.mysql.jdbc.Driver"
url="XXXX"/>
<Resource name="jdbc/emotifi_web" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="XXXX" password="XXXX"
driverClassName="com.mysql.jdbc.Driver"
url="XXXX"/>
</Context>

ClassNotFoundException,Missing ServletContainerAdaptor

I am creating one REstfulwebservices project. When ever I am going to run this
project then I am getting this error. ClassNot FoundException...ServletAdaptor
problem.
SEVERE: Servlet /XybuyProject threw load() exception
java.lang.ClassNotFoundException:
com.sun.jersey.server.spi.container.servlet.ServletAdaptor
at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:996)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4834)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5155)
at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5150)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
This is 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>FirstProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index2.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>
com.sun.jersey.server.spi.container.servlet.ServletAdaptor
</servlet-class>
<init-param>
<param-name>com.sun.jersy.config.property.packages</param-name>
<param-value>com.xybuy.webservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
</web-app>
can you tel me what is the problem behind it.....
Based on the stacktrace, you seem to be using Tomcat, which is a barebones JSP/Servlet container and not a full fledged Java EE container. Jersey is a JAX-RS implementation which is usually already bundled in fullfledged Java EE application servers such as Glassfish. Perhaps you were reading a Jersey book/tutorial or downloading a sample targeted on Glassfish or another Java EE container and thus it will be assumed that you don't need to install it separately.
However, on Tomcat, Jersey is thus not bundled and you will get ClassNotFoundExceptions on Jersey classes when declaring them in web.xml. You need to download the Jersey JAR file(s) separately and drop them in /WEB-INF/lib folder of the webapp.

setting the ContextPath in Jetty running under ServiceMix (OSGI)

Here is my brief background of environment.
I am trying to convert a myapp WAR to an OSGi compliant by making the MANIFEST.MF as shown below, and wanted to deploy this war in ServiceMix an OSGi based container.
MANIFEST.MF
Manifest-Version: 2.0
Bundle-SymbolicName: myapp
Bundle-Version: 2.1.dev
Bundle-Name: Jetty.myapp
Bundle-Vendor: ABC
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Bundle-ClassPath: .,WEB-INF/lib, WEB-INF/classes, xforms/xsltforms
Web-ContextPath: myapp
Webapp-Context: myapp
Import-Package: javax.servlet
Built-By: root
Project-Name: ABC_PRJ
Project-Version: 2.1.dev
Project-Build: ${DSTAMP}
SVN-Revision: ${svn.revision}
The ContextPath is getting set to null, where should I set the contextPath and how? You may observe the 'null' string in the FileNotFound Exception.
log:
java.io.FileNotFoundException: null/WEB-INF/myForms-config.xml (No such file or
directory)
My web.xml in myapp is :
<contextPath>/</contextPath>
<context-param>
<param-name>myForms.configfile</param-name>
<param-value>WEB-INF/myForms-config.xml</param-value>
</context-param>
Is it the problem with myapp or jetty or serviceMix? Any Clue?
You need to set the web-contextpath via the Manifest and not via the web.xml
for more information take a look at Pax-Web