在Jersey

时间:2018-02-21 05:19:33

标签: tomcat methods jersey startup invoke

您好,                 我试图在tomcat服务器启动时调用一个方法。我正在使用Jersey restful web服务     Macen Dependencies:

<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:Web应用程序中的Servlet [jersey-serlvet]   [/ FlightAvailabilityPredictor]抛出了load()异常   抛出java.lang.ClassNotFoundException:   com.sun.jersey.spi.container.servlet.ServletContainer at   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)     在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)     在   org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:508)     在   org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:489)

1 个答案:

答案 0 :(得分:0)

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

QLabel

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

<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>

下面创建了一个类
<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19</version>
    </dependency>