简体   繁体   中英

Java web application does not start on Tomcat 8.5 or 9 using Jersey 2.26

I'm trying to deploy a simple Java (1.8) web application to Tomcat 9 (also tried on 8.5) using Jersey 2.26 built using Maven. I do not see any errors while building but deploying to Tomcat, the application does not start.

在此处输入图片说明

I was referring to the options as listed here https://jersey.github.io/documentation/latest/deployment.html#deployment.servlet.3

Errors in Tomcat logs/catalina.out

17-Aug-2018 21:46:06.062 SEVERE [http-nio-8080-exec-39] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive [/Users/konathal/Software/apache-tomcat-9.0.10/webapps/jaq-stack-webapp.war] java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [

Caused by: java.lang.NoClassDefFoundError: javax/ws/rs/core/Application at org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer.addServletWithDefaultConfiguration(JerseyServletContainerInitializer.java:240)

pom.xml

 <dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-servlet</artifactId>
   <version>2.26</version>
 </dependency>
 <dependency>
   <groupId>org.glassfish.jersey.core</groupId>
   <artifactId>jersey-server</artifactId>
   <version>2.26</version>
 </dependency>
 <dependency>
   <groupId>org.glassfish.jersey.inject</groupId>
   <artifactId>jersey-hk2</artifactId>
   <version>2.26</version>
 </dependency>

web.xml

<servlet>
  <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
  <servlet-name>javax.ws.rs.core.Application</servlet-name>
  <url-pattern>/service/*</url-pattern>
</servlet-mapping>

Servlet class

@Path("/dbservice")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MongoServlet {

What am i missing?

Looks like a missing jar. Actually the stacktrace message is pretty clear on the fact that javax/ws/rs/core/Application cannot be found.

Add this dependency to your pom.xml clean and rebuild the project and I suppose you should be OK.

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1</version>
</dependency>

Like @Aris_Kortex mentioned the issue was with missing

The solution was to remove <scope>provided</scope> from the maven dependency in pom.xml

The dependency in my pom.xml before

<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>javax.ws.rs-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

Corrected dependency

<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>javax.ws.rs-api</artifactId>
  <version>2.1</version>
</dependency>

Tomcat does not provide the jar and has to explicitly added by the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM