集成 Spring 和 jersey 时在 web.xml 中指定 context-param [英] Specifying context-param in web.xml while integrating Spring and jersey

查看:49
本文介绍了集成 Spring 和 jersey 时在 web.xml 中指定 context-param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有一个关于这个主题的问题但没有得到答复 当向 <context-param> 添加值时应用程序不起作用在 web.xml 中使用 Maven 的 Spring MVC Web 应用程序

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

我正在尝试集成 Spring 和 jersey,当我添加 context-param 标记来指定 applicationContext.xml 时,应用程序将不会加载主页(webContent 文件夹下的 index.jsp),如果该标记被移除,应用程序将加载主页,但我将无法指定 spring(applicationContext.xml) 的配置

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)

这是我的 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>

这是我的 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>

这是我的 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 在classpath(即src文件夹)下,web.xml在WEB-INF下,index.jsp在WebContent下

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

感谢您的帮助!

推荐答案

基本问题是你混淆了 xml 和 annotationConfigs.您需要知道 contextClasscontextConfigLocation 上下文参数之间的关系以及 ContextLoaderListener 将使用它.

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 :指的是配置类.你在这里提到它作为 org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  2. contextConfigLocation :这是 spring 引导程序搜索配置的位置.您将 contextClass 指定为 AnnotationConfigWebApplicationContext,为此您应该提供 Spring bean 配置类所在的包名称.我假设您已经用 @Configuration
  3. 注释了这样的类
  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

如果您没有这样的@Configuration 注释类,请删除 contextClass 并仅将 contextConfigLocation 参数与您的 xml 路径作为值.默认 ContextLoaderListener 以 xml 为基础.这是 从基于 xml 的 spring 迁移到 java 配置

If you have no such @Configuration annotated class remove contextClass and put only contextConfigLocation param with your xml path as value. By default ContextLoaderListenertake 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.

接下来我仍然感到困惑,为什么您使用 RequestContextListener 来处理 REST 应用程序.如果根据 其文档 将其删除.如果你想使用它示例会有所帮助

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

还可以在这里找到一些有用的细节

这篇关于集成 Spring 和 jersey 时在 web.xml 中指定 context-param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆