Hi I am trying to build a Simple JaxRs web service on JBoss developer studio and Wildfly 11 as application server but i am getting following error while i am trying to deploy my maven project :
Failed to start service
jboss.undertow.deployment.default-server.default-host."/JaxRsTest-0.0.1-SNAPSHOT":
org.jboss.msc.service.StartException in service
jboss.undertow.deployment.default-server.default-host."/JaxRsTest-0.0.1-SNAPSHOT":
java.lang.NoSuchMethodError:
org.apache.tomcat.util.descriptor.DigesterFactory.newDigester(ZZLorg/apache/tomcat/util/digester/RuleSet;Z)Lorg/apache/tomcat/util/digester/Digester;
Also i want to inform you that the project is working fine when i use apache tomcat 9 However it throws above mentioned error when I switch to wildfly 11. I am new to java and Specially web service and just started working for a company which uses Jboss eap server which is quite same as wildfly and unfortunately i am receiving same error there as well while working on a web service assignment.
For more reference below is my pom.xml file :
<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>JaxRsServiceTest</groupId>
<artifactId>JaxRsServiceTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
and my web.xml file content is :
<?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" id="WebApp_ID" version="3.1">
<display-name>JaxRsServiceTest</display-name>
<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.test.jaxrs.service</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>
and my java class within package com.test.jaxrs.service is
package com.test.jaxrs.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/test")
public class JaxRsTest {
#GET
#Path("/hello/{msg}")
#Produces(MediaType.TEXT_PLAIN)
public String processRequest(#PathParam(value="msg")String message)
{
return "Hello : " + message;
}
}
Please let me know where i am going wrong with the wildfly.... and Thanks in advance
Wildfly and EAP are a full JEE servers - there is no need to include Jersey dependencies in your pom.xml. Tomcat required that because it is primarily a servlet/JSP engine. Your service looks good but your pom.xml and web.xml are the result using Tomcat. Additionally, you do not need a web.xml any longer if you don't want and you certainly don't need to use Jersey to map it.
You'll want one more file - the Application class that gets things started. It looks something like:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* Used to bootstrap JAX-RS. Otherwise this class is
* not directly used.
*/
#ApplicationPath("/rest")
public class RestApplicationConfig extends Application {
// intentionally empty
}
It can go anywhere in your source tree. Note that it takes over the /rest path which is similar to what you had in your web.xml.
The pom.xml below will create a .war file that you can deploy to Wildfly. This is about as simple as it gets and again, remove your web.xml for now.
pom.xml
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JaxRsServiceTest</groupId>
<artifactId>JaxRsServiceTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.12.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Related
I am creating a simple webservice project that pulls data from db on countries and displays it in a JSON form, My project does not throw any error and builds successfully but it is unable to find deployed resource.
WorldInformation.java class
package com.webservices;
import java.util.List;
import com.webservices.models.Country;
import com.webservices.services.WorldInformationService;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
#Path("/worldinformation")
public class WorldInformation {
WorldInformationService worldInformationService = new WorldInformationService();
#GET
#Path("/getCountries")
#Produces(MediaType.APPLICATION_JSON)
public List<Country> getCountries(){
System.out.println("reached point 1");
List<Country> countryList = worldInformationService.getCountries();
return countryList;
}
#POST
#Path("/setCountry/{country}/{countryCode}")
#Consumes(MediaType.TEXT_PLAIN)
public void setCountry(#PathParam("country") String country,#PathParam("countryCode") String countryCode){
worldInformationService.setCountry(country,countryCode);
}
}
This is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And this is what pom.xml looks like
<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>com.webservices</groupId>
<artifactId>WorldInformation</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WorldInformation</name>
<build>
<finalName>WorldInformation</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
This is error snapshot
I can't seem to figure out how to get this resolved and what's going wrong, will deeply appreciate your help on this.
I think you need to add #ApplicationPath in your jax-rs application as well.
Note: The URL is also missing the context path.
For example: http://localhost:8080/<context-path>/servlet/path
From Oracle Docs :
The #ApplicationPath annotation is used to define the URL mapping
for the total application. The path specified by #ApplicationPath is the
base URI for all resource URIs specified by #Path annotations in the
resource class.
For example : Add a class WorldInformationApp annotated with #ApplicationPath in the project.
#ApplicationPath("/worldinfo")
public class WorldInformationApp extends Application {
}
Try to access the resource with this url : http://localhost:8080/<context-path>/worldinfo/worldinformation/getCountries
Note : I think you are using wrong libraries for jax-rs app.
For example : javax.ws.rs.* should be used instead of jakarta.ws.rs.*
Thanks for your valuable feedbacks, here is what was worked for me. As someone suggested. I was not looking at correct point. I was missing application name in the url I was trying to connect to. Though I ran into other problems but I managed to solve them and now its working for me.
localhost:8080/WorldInformation/worldinformation/getCountries
Thanks for your valuable help on this.
I am trying to make a RESTFUL JAVA WEB SERVICE using eclipse and maven in Apache Tomcat. Everything seems fine to me and it is not showing any error but when open the URL it shows 404 Error.
I am trying to call it via this URL http://localhost:8080/first/rest
WEB.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>first</display-name>
<servlet>
<servlet-name>firstapp</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.first.pkg</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>firstapp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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>task</groupId>
<artifactId>first</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>first Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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>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>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
</project>
RestServ.java
package com.first.pkg;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
#Path("/")
public class RestServ {
#GET
#Produces("text/html")
public Response getStartingPage()
{
String output = "<h1>Hello World!<h1>" +
"<p>RESTful Service is running ... <br>Ping # " + new Date().toString() + "</p<br>";
return Response.status(200).entity(output).build();
}
}
<display-name>first</display-name>
is a description part and I don't think you can call your web service via this name. Instead, try calling it like
http://localhost:8080/{nameOfYourWARFileHere}/rest
Also make sure to put rest into your #Path("/") annotation in RestServ.java right after the slash otherwise the request mapping won't work.
I am sending a GET request to my StoryBoardResource, class responsible for generating JSON format data, using POSTMAN app. The associated method consumes nothing but produces Application/JSON data: #Produces(MediaType.APPLICATION_JSON).
I am recieving 500 internal server error on POSTMAN though there is nothing showing up on my IDE's console as am logging some statements too for debugging purpose.
If there had been some jar file issue it must have thrown an error for no MesageBodyWriter found ...right? I have updated pom.xml to include JSON dependency and the jar is also there.
Here is my StoryBoardResource class to which the request is delegated:
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.harsh.webapp.storyBoard.model.StoryBoardModel;
import org.harsh.webapp.storyBoard.service.StoryBoardService;
#Path("/authenticateUser")
public class StoryBoardResource {
private StoryBoardService sts = new StoryBoardService();
#GET
#Produces(MediaType.APPLICATION_JSON)
public StoryBoardModel authUser(#QueryParam("username") String username, #QueryParam("password") String password){
return sts.authUser(username, password);
}
}
which calls authUser method on my service class StoryBoardService :
public StoryBoardModel authUser(String username, String password){
System.out.println("In here");
if(username.equals(map.get(username).getUsername()) && password.equals(map.get(username).getPassword())){
System.out.println("true");
return map.get(username);
}
return map.get(username);
}
Could this be bcoz of some jar or Jersey version conflict?
Here is 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.harsh.webapp</groupId>
<artifactId>storyBoard</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>storyBoard</name>
<build>
<finalName>storyBoard</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
And here is a snapshot of Maven Dependencies
Sorry for such a long post but I thought I should explain things clearly.
Please help on finding what could I be doing wrong here?
And web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>org.harsh.webapp.storyBoard</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>
</web-app>
I worked it out and it appeared so that I missed adding a NO-Argumentconstructor in a model class.
public someClass(){};
Always remember to add a No-Arg constructor whenever you create a model class for your Maven-Jersey application.
And it worked!
However, I am not clear on why would a no-arg constructor make the application run or fail!!
If any body know the answer to this. Please share your knowledge!!
I always end up with a 404 response.
Resource not found. Testing with tomcat
v8.0. Below are the
details.
URL : http://localhost:8080/ESTServer/rest/message/hello
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>ESTServer</groupId>
<artifactId>ESTServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.13.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.13.Final</version>
</dependency>
</dependencies>
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>ESTServer</display-name>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.mota.rest.CaDistributionApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Service code :
package com.mota.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/message")
public class CaDistributionService {
#GET
#Path("/{param}")
public Response printMessage(#PathParam("param") String msg) {
String result = "Restful example : " + msg;
return Response.status(200).entity(result).build();
}
}
project structure
CaDistributionApplication.java
package com.mota.rest;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
import com.mota.rest.CaDistributionService;;
public class CaDistributionApplication extends Application{
private Set<Object> singletons = new HashSet<Object>();
public CaDistributionApplication() {
singletons.add(new CaDistributionService());
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
What am I missing here ?
Please help !!!!
I just tried restarting the eclipse.
Multiple clean/build/refresh/install.
Checking the Target folder for generated
war and classes. Launching tomcat in debug mode.
But basically didn't change anything in the code.
And yes it works finally.
I have a very simple java web application where I use maven for build process and try deploying it on Tomcat.
I am using tomcat 7.0.5332, maven version 2.2.1.
Here is my very simple web app:
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>com.tugay.anotherwebapp</groupId>
<artifactId>another-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>another-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
<build>
<finalName>another-webapp</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
And here is my web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>MyServletName</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServletName</servlet-name>
<url-pattern>/index.html</url-pattern>
</servlet-mapping>
</web-app>
And MyServlet class:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* User: Tugay
* Date: 4/5/14
* Time: 9:14 PM
*/
public class MyServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws IOException {
System.out.println("Hello World!");
}
}
So
mvn clean install
works fine and the war is generated. But when deployed on Tomcat I get this:
- jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: /javax/servlet/Servlet.class
What is it that I am doing wrong?
I think you should set this dependency to provided:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Because it will already be in the tomcat shared classloader
If you look at the Servlet Spec 3.0, section 10.7.2 - you will find that the problem seems to be that you are trying to load Servlet class that is already provided by the container (Tomcat). Please, check whether servlet-api is in your war(it should not be) and use provided scope for javaee-api.
It's normal. Servlet containers provide their own servlet-api implementation classes. So classes included in your war (from javaee-api) clash with ones provided by tomcat.
To avoid this, set the scope of the javaee-api dependency to provided.
This way classes provided by that dependency will be visible at compile time but the jar itself won't be included in the resulting war.