简体   繁体   中英

Specifying context-param in web.xml while integrating Spring and jersey

I am aware that there is already a question asked on this topic but is unanswered Application not workig when adding value to <context-param> in web.xml in Spring MVC Web Application Using Maven

I am trying to integrate Spring and jersey , when i add context-param tag to specify the applicationContext.xml the application will not load the homepage(index.jsp under webContent folder) ,if that tag is reemoved the application loads the homepage but thrn i will not be able to specify the configuration for spring(applicationContext.xml)

here is my web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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.im.hellocontroller</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
 <context-param>  
 <param-name>contextClass</param-name>  
 <param-value>
 org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>  
 </context-param>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
 <listener-     class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-  class>org.springframework.web.context.request.RequestContextListener</listener- class>
</listener> 
</web-app>

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Inventory-Management</groupId>
<artifactId>Inventory-Management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
    </repository>
</repositories>

<dependencies>

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

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

    <!-- Jersey -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.19</version>
    </dependency>

    <!-- Spring dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <!-- Jersey + Spring -->
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>1.19</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--  Hibernate dependencies -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.1.Final</version>
    </dependency>

    <!-- MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.36</version>
    </dependency>

</dependencies>



<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
        </plugin>
    </plugins>
</build>

here is my applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.im.hellocontroller"/>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/imanagement" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean> 

 <bean id="sessionFactory"  
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
<property name="dataSource" ref="dataSource" />  
<property name="annotatedClasses">    
        <list>    
            <value>com.im.beans.User</value>    
        </list>    
    </property>   
<property name="hibernateProperties">  
<props>  
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
<prop key="hibernate.current_session_context_class">thread</prop> 
<prop key="hibernate.show_sql">true</prop>  
<prop key="hibernate.hbm2ddl.auto">update</prop>

 </props>  
</property>  
 </bean> 

</beans>

applicationContext.xml is in the classpath (ie src folder),web.xml in under WEB-INF,index.jsp is under WebContent

Thanks for any help !

The basic problem is you mixed up xml and annotationConfigs. You need to know the relation between contextClass and contextConfigLocation context params and how ContextLoaderListener will make use of it.

  1. contextClass : refers the configuration class. You mentioned here it as org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  2. contextConfigLocation : this is the location where it spring's bootstraper will search for config. You specified contextClass as AnnotationConfigWebApplicationContext, for that you should give the package name where your Spring bean configuration class resides.I assume you have such class annotated with @Configuration

If you have no such @Configuration annotated class remove contextClass and put only contextConfigLocation param with your xml path as value. By default ContextLoaderListener take the xml based. Here is an example of migrate to java config from xml based spring

You can find a ready to adapt example of jersey integration here .

Next thing I still confused, why you are using RequestContextListener for REST application. If you don't have any special purpose as per its documentation remove it. If you want to use it this example will help

Also find some useful detail regarding the question here

我终于找到了解决我问题的方法,我混合了jersey1.x和jersey2.x的Maven依赖项(即sun和glassfish的依赖项)

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