org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException on server startup - java

I'm trying to run REST interface using Tomcat and Jersey with no success, I'm getting annoying error at server startup
Oct 13, 2014 2:53:45 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Jersey Web Application as unavailable
Oct 13, 2014 2:53:45 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at (...)
I'm using:
OS X 10.8.5
IntelliJ IDEA 13.1.5
Tomcat 7.0.56
Java JDK 7 Update 67
Maven 3.0.4
Jersey 2.13
My pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javacodegeeks.enterprise.rest.jersey</groupId>
<artifactId>JerseyJSONExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!--<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18</version>
</dependency>-->
</dependencies>
</project>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>JAVA REST TEST</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>RestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<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>
</web-app>
I've checked similar questions but all of them was solved by changing Jersey version, but looks like I'm using correct one.
List of libraries generated by maven:
Maven: javax.annotation:javax.annotation-api:1.2
Maven: javax.validation:validation-api:1.1.0.Final
Maven: javax.ws.rs:javax.ws.rs-api:2.0
Maven: org.glassfish.hk2.external:aopalliance-repackaged:2.3.0-b10
Maven: org.glassfish.hk2.external:javax.inject:2.3.0-b10
Maven: org.glassfish.hk2:hk2-api:2.3.0-b10
Maven: org.glassfish.hk2:hk2-locator:2.3.0-b10
Maven: org.glassfish.hk2:hk2-utils:2.3.0-b10
Maven: org.glassfish.hk2:osgi-resource-locator:1.0.1
Maven: org.glassfish.jersey.bundles.repackaged:jersey-guava:2.13
Maven: org.glassfish.jersey.containers:jersey-container-servlet:2.13
Maven: org.glassfish.jersey.containers:jersey-container-servlet-core:2.13
Maven: org.glassfish.jersey.core:jersey-client:2.13
Maven: org.glassfish.jersey.core:jersey-common:2.13
Maven: org.glassfish.jersey.core:jersey-server:2.13
Maven: org.javassist:javassist:3.18.1-GA
Tomcat 7.0.56
Any ideas what I'm doing wrong?

Ok, found what was wrong. In my case I've added lib folder to WEB-INF (Project Structure->Artifacts, then in "Available Elements" select all of them, right click and select "Put into /WEB-INF/lib").

You can face this exception if you are setting up jersey 2 project first time in eclipse using maven plugin. Message is simple enough to identify the root cause the Jersey libraries are not in classpath.
Solution – Add Jersey Library in Deployment Assembly
Open your project’s deployment assembly configuration.
Add Build path jar files in assembly so that they can be added to lib
folder in final war file.
Deployment Assembly - Add Build Path Entries
Select Maven Dependencies
Now when you again run the project after building it, this will run fine and will be able to find org.glassfish.jersey.servlet.ServletContainer class.

Related

Tomcat server is running but its resources are not found

I´ve created an maven project with the jersey archetype and attached a tomcat server to it. My tomcat server is running, but when i try to access to a resource, it does not appear (Error 404:Not found,
The required resource is not available.)
I already have tried to change the server path to use tomcat installation option. Also, i added a project facet with a dynamic project module.
web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.telusko.pablo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
pom.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.26-b03</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I only expect to can access the resources and not get the error 404.

java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

I am building a web service using Java, Apache Cxf and Spring Frame work
Build: Maven
Deployment: Tomcat 8.0.42
The servlet used in web.xml is of version 2.5. When I deployed the WAR into tomcat and start the server I am getting the following Error. I have tried many of the solutions presented in the stack but none seems to be working. I have checked for the servlet-api.jar in maven dependencies and it is not present either. Also gave the servlet version in web.xml as 3.0, but it also didn't seem to resolve the issue. Need good inputs.
Thanks in advance
Apr 12, 2017 5:24:58 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SAMPLE-WS]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SAMPLE-WS]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
... 6 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;
at org.apache.tomcat.websocket.server.WsServerContainer.<init>(WsServerContainer.java:150)
at org.apache.tomcat.websocket.server.WsSci.init(WsSci.java:131)
at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:47)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
... 6 more
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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>SAMPLE-WS</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/sample-cxf-beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
POM.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaws.sample.ws</groupId>
<artifactId>SAMPLE-WS</artifactId>
<packaging>war</packaging>
<name>SAMPLE-WS</name>
<properties>
<project.wsdl.path>src/main/webapp/WEB-INF/WSDLs/Sample_1.wsdl</project.wsdl.path>
<target.wsdl.path>WEB-INF/WSDLs/Sample_1.wsdl</target.wsdl.path>
</properties>
<dependencies>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- Apache CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
</dependency>
</dependencies>
</project>
the method getVirtualServerName has been added in ServletContext in Servlet 3.1. Find the java doc's method getVirtualServerName
this problem can have at least 2 causes:
your servlet version is older that 3.1.
other jar has the servlet older version than 3.1
to solve it, you can try the below way.
I. to check your pom.xml whether there are the below code.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
if your pom.xml has the above code, it would still has that problem. you can do the second way.
II. to check your other jar has refer to the javax.servlet-api jar. for example, the org.apache.santuario has refer to the javax.servlet-api jar. the pom.xml:
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
</dependency>
but when you look at the maven dependencies, it refer to the javax.servlet-api jar whose version is 2.3 older than 3.1.
so you should exclude the 2.3 version. pom.xml:
<!-- exclude servlet-api 2.3 jar-->
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- servlet-api 3.1 version has getVirtualServerName() -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

Apache Tomcat 9 - HTTP Status 404 on browser

I'm trying to create a simple web service, but I'm getting 404 when I try to go to the URL.
Here's the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>Test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Here's pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>1</groupId>
<artifactId>1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
</dependencies>
</project>
And here's the basic rest class:
package Test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
/**
* Created by * on 4.02.2017.
*/
#Path("/api")
public class Test {
#GET
#Path("/get/test")
public String getSmth(String request) {
return "Testing";
}
}
And when I go to:
http://127.0.0.1:8080/Test/rest/api/get/test
I get:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/9.0.0.M17
When I go to the Tomcat app manager I can see my app running there. Am I doing something really wrong or missing something?
Thanks,
Eerik
In the code you are using glassfish server and not tomcat. In the IDE you are using try to use glassfish server instead of tomcat with the same code.

Jersey ClassNotFoundException when contextInitialized

When I connect to the selected servlet, it throw me a ClassNotFoundException. I am using eclipse neon with Tomcat8 and jersey RESTful framework. Its possible that I have the servlet conf in wrong in the web.xml?
Error showed:
SEVERE: Allocate exception for servlet jersey
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
My web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>org.CTAG.DATEX2REST</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>
<display-name>CTAG DATEX2</display-name>
<servlet>
<servlet-name>jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.CTAG.DATEX2REST</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
com.CTAG.application.Init
</listener-class>
</listener>
</web-app>
The POM.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.CTAG.DATEX2REST</groupId>
<artifactId>org.CTAG.DATEX2REST</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>org.CTAG.DATEX2REST Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>org.CTAG.DATEX2REST</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Your web.xml content is incorrect, it matches with Jersey 2.x while you obviously use Jersey 1.x, indeed the package names start with com.sun.jersey in Jersey 1.x while in Jersey 2.x they start with org.glassfish.jersey.
Check here what should be the content of your web.xml in case of Jersey 1.x..
<servlet>
<servlet-name>jersey</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>org.CTAG.DATEX2REST</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
Or switch to Jersey 2.x.
Fixing the dependencies
You are mixing Jersey 1.x (com.sun.jersey) and Jersey 2.x (org.glassfish.jersey).
I understand you wan't to use Jersey 2.x, so remove:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
And then add to your pom.xml
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older
than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
</dependency>
Deploying in a Servlet 2.x container (like Tomcat 6)
Jersey integrates with any Servlet containers supporting at least Servlet 2.5 specification. For these environments, you have to explicitly declare the Jersey container servlet in your web application's web.xml deployment descriptor file:
<web-app>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
...
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
The content of the <init-param> element will vary depending on the way you decide to configure Jersey resources.
Deploying in a Servlet 3.x container (like Tomcat 7 and above)
In a Servlet 3.0 container, you just need to implement a custom Application / ResourceConfig subclass. For simple deployments, no web.xml is necessary at all. Instead, an #ApplicationPath annotation can be used to annotate the custom Application / ResourceConfig subclass and define the base application URI for all JAX-RS resources configured in the application:
#ApplicationPath("api")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("org.foo.rest;org.bar.rest");
}
}
For other deployment options, have a look at the documentation.

Different behaviour using mvn jetty:run or jetty standalone

I have a project with multiple modules.
Two of them generate war files.
One war file is a REST application and provides a couple of resources.
The Other war file is a Angular JS web application (static content only) to talk to the REST backend.
For demo purposes I'd like to deploy both war-files very easily with mvn jetty:run
For development purposes I'd like to deploy them from my IDE (e.g. Eclipse Servers View).
When I do the deployment on a single Jetty Server (v9.0.7.v20131107) manually by starting the server and copiing the war-files to the deployment folder everything comes up.
When starting the jetty by mvn jetty:run both war files get deployed, but somehow the REST Resources do not get deployed.
I am using Jersey 2. When deploying manually I get a log message like
Nov 14, 2013 10:44:37 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4 2013-10-24 18:25:49...
However this message is not been shown when starting with mvn jetty:run. Therefore I assume that Jersey does not kick in.
For Dependency-Injection I use spring.
This is the parent pom in /pom.xml with the jetty-maven-plugin configuration
<project ...>
...
<build>
<plugins>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module1/target/module1-${project-version}.war</war>
<contextPath>/module1</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module2/target/module2-${project.version}.war</war>
<contextPath>/module2/contextPath>
</contextHandler>
</contextHandlers>
</configuration>
</plugins>
</build>
...
</project>
This is the pom for module1 (the REST module)
<parent>
...
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<springVersion>3.1.4.RELEASE</springversion>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4</version>
</dependency>
<!-- Dependencies to internal modules -->
...
<!-- Depenencies to internal modules END -->
</dependencies>
</project>
This is the web.xml for module1
<web-app ...
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
This is the applicationContext.xml for module1
<beans ...>
<context:annotation-config/>
<context:component-scan base-package="com.stackoverflow.zip"/>
<aop:aspectj-autoproxy/>
</beans>
This is the Module1Application class for module1
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
#ApplicationPath("/rest")
public class Module1Application extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Resource1.class);
classes.add(Resource2.class);
classes.add(MultiPartFeature.class);
return classes;
}
}
This is the pom for module2 (the AngularJS app)
Very light :)
<project ...>
<parent>
...
</parent>
...
<modelVersion>4.0.0</modelVersion>
<artifactId>module2</artifactId>
<packaging>war</packaging>
</project>
Do you have any idea why the Jersey Application does not get instantiated while running with mvn jetty:run but when running it manually?
I appreciate any input on this topic.
Kind regards
- zip
Your javax.servlet version is 2.5 in pom.xml, but 3.0 in web.xml. Try upgrading it to 3.0 in pom.xml. Is it possible that the IDE provides a servlet 3.0 for you?
Also, since Jetty provides a javax.servlet container, try putting the Servlet Api into provided scope in pom.xml. Something like this:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<!-- http://stackoverflow.com/a/15601606 http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope -->
<!-- This allows us to compile the application locally but does not include the jar in the package step -->
<scope>provided</scope>
</dependency>
It's possible that you want to add a <load-on-startup>1</load-on-startup> element to your web.xml. Check out http://tutorials.jenkov.com/java-servlets/web-xml.html#load-on-startup
That's whole tutorial is pretty good.
Disclaimer: I'm not familiar with what the Spring dependencies are doing to help your application bootstrap itself. You can always try removing dependencies on a separate branch, and adding them back in as you get things running
I haven't had a chance to play with Jersey 2.x so far, but from what I see in your web.xml, it doesn't look like you have Jersey setup correctly.
In a non-Spring application, you would have something like the following:
<web-app ...>
...
<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.your.foo.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
In a Spring-based one, you would use:
<web-app ...>
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.foo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
Furthermore, check that you have these Maven dependencies:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

Categories