我目前正在尝试将我的服务器设置为在tomcat上使用jersey,spring和hibernate。目前,项目使用maven构建并在tomcat上部署。当我在调试模式下运行并调用一些GET方法时,调用com.x.y.z.request包中的正确@GET方法,但此类中的自动服务的服务类为null,我无法弄清楚原因。我怀疑这是一个配置问题,但我对此有点新,不确定我哪里出错了。
我在服务器启动时在控制台中得到这个
....
Sep 13, 2013 11:18:56 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
INFO: Using default applicationContext
Sep 13, 2013 11:18:56 PM com.sun.jersey.spi.spring.container.SpringComponentProviderFactory registerSpringBeans
INFO: Registering Spring bean, testRequestService, of type com.x.y.z.TestRequestService as a root resource class
....
所以看起来正在初始化请求bean
的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/springjee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan
base-package="com.x.y.z.request, com.x.y.z.service, com.x.y.z.dao" />
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/db" />
<property name="username" value="user" />
<property name="password" value="password" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="persistenceUnitName" value="serverPersistenceUnit" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
web.xml(我写了*作为&#39; *&#39;因为它正在评论文本块)
<?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"
version="2.5">
<display-name>MyWebApp</display-name>
<description>
My Web App
</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</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>
<servlet>
<servlet-name>Jersey Spring Web Application</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.x.y.z.request</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Spring Web Application</servlet-name>
<url-pattern>/webresources/'*'</url-pattern>
</servlet-mapping>
<!-- JAX RS config -->
<filter>
<filter-name>Jersey Filter</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.feature.Trace</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.ImplicitViewables</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.x.y.z.request</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Jersey Filter</filter-name>
<url-pattern>/'*'</url-pattern>
</filter-mapping>
</web-app>
的persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="serverPersistenceUnit">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
TestRequestService.java
@Component
@Path("/test")
public class TestRequestService
{
@Autowired
private IDelegate delegate;
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public List<IObject> get(@PathParam("id") int id)
{
return delegate.get(id);
}
}
IBusinessDelegate.java
@Service
public class Delegate extends AbstractDelegate<IObject> implements IDelegate
{
@Autowired
private IDAO<IObject> dao;
@Override
public List<IObject> get(int id)
{
dao.get(id);
}
}
DAO.java
@Repository
public class DAO extends AbstractDAO<IObject> implements IDAO<IObject>
{
@Override
public List<IObject> get(int id)
{
//some query here
}
}
AbstractDAO.java
public abstract class AbstractDAO<T> implements IDAO<T>
{
@PersistenceContext
protected EntityManager entityManager;
public abstract List<T> get(int id);
}
答案 0 :(得分:2)
问题的最可能原因是Jersey未配置为使用spring应用程序上下文来获取对象实例。所以Jersey正在创建TestRequestSerivce的实例,这意味着Jersey会忽略@Autowired,因为它不理解它。
您是否将Jersey servlet配置为使用spring来获取控制器实例。看到 http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example特别注意到jersey的servlet应该知道。
<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.mkyong.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
答案 1 :(得分:0)
事实证明,这只是因为我使用错误的网址进行测试。您可以在我的web.xml中看到我的servlet-mapping的URL模式是/ webresources / *。如果我通过在浏览器中输入localhost:8080 / my-webservice / test / 1进行测试,它会在“/ test”请求bean中点击我的断点,但是spring没有自动装配字段(可能是因为它是错误的URL模式这件针织衫/弹簧小屋)。当我开始输入localhost:8080 / my-webservice / webresources / test / 1时,一切都开始正常了。这是一个非常愚蠢的错误,但我只是假设如果球衣正在接收请求,那么URL是正确的,也许有人可以解释为什么错误的URL发生这种情况。