gpt4 book ai didi

tomcat - 在 Jersey 中调用 tomcat 启动的方法

转载 作者:行者123 更新时间:2023-11-28 23:16:20 26 4
gpt4 key购买 nike

您好, 我试图在 tomcat 服务器启动时调用一个方法。我正在使用 Jersey Restful 网络服务。
Macen 依赖项:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.2</version>
</dependency>

我的 WEB.XML 文件:-

            <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>FlightAvailabilityPredictor</display-name>
<servlet>
<servlet-name>jersey-serlvet</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.travelport.h2o.availabilitypredictor</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<listener>
<listener-class>
com.travelport.h2o.availabilitypredictor.listener.InitializeListner
</listener-class>
</listener>

</web-app>

我的听众类:-

        package com.travelport.h2o.availabilitypredictor.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class InitializeListner implements ServletContextListener {

@Override
public final void contextInitialized(final ServletContextEvent sce) {
// method to invoke
}

@Override
public final void contextDestroyed(final ServletContextEvent sce) {

}
}

我收到以下异常:-

SEVERE: Servlet [jersey-serlvet] in web application [/FlightAvailabilityPredictor] threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:508) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:489)

最佳答案

找到解决方案:- 如下更改我的 Web.xml

<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>FlightAvailabilityPredictor</display-name>
<servlet>
<servlet-name>jersey-serlvet</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.travelport.h2o.availabilitypredictor</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<!-- <listener>
<listener-class>
com.travelport.h2o.availabilitypredictor.listener.InitializeListner
</listener-class>
</listener> -->
<servlet>
<servlet-name>InitializeListner</servlet-name>
<servlet-class>com.travelport.h2o.availabilitypredictor.listener.InitializeListner</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

</web-app>

将我的 Maven 依赖项从 1.19.2 更改为 1.19

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>

在下面创建了一个类

  import javax.servlet.http.HttpServlet;
@SuppressWarnings("serial")
public class InitializeListner extends HttpServlet {
public void init() {
System.out.println("INSIDE INIT METHOD");
}
}

关于tomcat - 在 Jersey 中调用 tomcat 启动的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48898645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com