我试图实现RESTEasy + Spring,但@Autowire无法正常工作。我找到了一些参考资料,我需要使用org.jboss.resteasy.plugins.spring.SpringContextLoaderListener并禁用resteasy自动扫描。我已经尝试了,但现在我的网络服务无法识别。我做错了什么?这是我的web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>APF Web Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- REST configuration -->
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<!-- RESTEasy <-> Spring connector (RESTEasy can access Spring beans) -->
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/json/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/json</param-value>
</context-param>
<!--<context-param>-->
<!--<param-name>resteasy.resources</param-name>-->
<!--<param-value>ApplicationSvc</param-value>-->
<!--</context-param>-->
<!-- End of REST configuration -->
</web-app>
这是我的网络服务
@Service
@Path("/json")
public class ApplicationSupportSvc {
@Autowired
private MobileSurveyApkRepo mobileSurveyApkRepo;
@GET
@Path("/latest")
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public ApkResponse getLatestApk() {
return mobileSurveyApkRepo.getLatestApk();
}
}
这是我的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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="some.package" />
<context:annotation-config />
<aop:config proxy-target-class="true"></aop:config>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/SomeConn" />
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
<![CDATA[
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.default_schema=FIN
hibernate.hbm2ddl.auto=update
hibernate.hbm2ddl.keywords=auto-quote
hibernate.show_sql=true
javax.persistence.validation.mode=none
]]>
</value>
</property>
<property name="packagesToScan">
<list>
<value>some.package</value>
</list>
</property>
<property name="mappingLocations">
<list>
<value>classpath:jbpm.execution.hbm.xml</value>
<value>classpath:jbpm.history.hbm.xml</value>
<value>classpath:jbpm.task.hbm.xml</value>
<value>classpath:jbpm.repository.hbm.xml</value>
<value>classpath:jbpm.identity.hbm.xml</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" />
<!-- End of Hibernate session factory -->
<!-- jBPM4 Bean Definitions -->
<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper">
<property name="jbpmCfg">
<value>jbpm.cfg.xml</value>
</property>
</bean>
<bean id="processEngine" factory-bean="springHelper"
factory-method="createProcessEngine" />
<bean id="repositoryService" factory-bean="processEngine"
factory-method="getRepositoryService" />
<bean id="executionService" factory-bean="processEngine"
factory-method="getExecutionService" />
<bean id="taskService" factory-bean="processEngine"
factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine"
factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine"
factory-method="getManagementService" />
<!-- End of jBPM4 Bean Definitions -->
</beans>
我使用RESTEasy 3.0.6和Spring 4.0.3,使用JBOSS EAP 6.2
答案 0 :(得分:2)
RESTEasy和Spring 4存在一个错误:https://issues.jboss.org/browse/RESTEASY-1012
根据问题的说明创建您自己的ContextLoaderListener
实施。
答案 1 :(得分:1)
此错误已在Resteasy 3.0.9中修复。最终: https://github.com/resteasy/Resteasy/blob/master/jaxrs/resteasy-spring/src/main/java/org/jboss/resteasy/plugins/spring/SpringContextLoaderListener.java
现在您可以使用以下内容:
... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <org.springframework-version>4.1.1.RELEASE</org.springframework-version> <spring-security-config-version>3.2.4.RELEASE</spring-security-config-version> <servlet-version>3.1.0</servlet-version> <org.jboss.resteasy-version>3.0.9.Final</org.jboss.resteasy-version> <javax.inject-version>1</javax.inject-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <type>jar</type> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet-version}</version> <scope>provided</scope> </dependency> <!-- JSR 330: Dependency Injection for Java --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>${javax.inject-version}</version> </dependency> <!-- JAX-RS --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-spring</artifactId> <version>${org.jboss.resteasy-version}</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-servlet-initializer</artifactId> <version>${org.jboss.resteasy-version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>${org.jboss.resteasy-version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>${org.jboss.resteasy-version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>${org.jboss.resteasy-version}</version> <scope>compile</scope> </dependency> </dependencies>
<listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <listener> <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class> </listener> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> <servlet> <servlet-name>rest-servlet</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <!-- NEVER map servlet to root context "/*" ! --> <servlet-mapping> <servlet-name>rest-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
// JSR 330: Dependency Injection for Java @Named @Path("api/v1") @Produces({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_JSON})
public class SomeResource {
@Inject
@Named("someService")
private SomeServiceInterface someService;
@Inject
@Context
private HttpServletRequest request;
@GET
@Path("{msg}")
public Response ehlo(@PathParam("msg") String msg) {
return Response.ok().entity(msg).build();
}
}
答案 2 :(得分:0)
您还需要一个弹簧配置文件。 RESTEasy期望在WEB-INF目录中有一个applicationContext.xml文件。
此文件应包含类似于:
的内容<context:component-scan base-package="
my.package"/>
答案 3 :(得分:0)
有两种方法可以解决这个问题 1)使用some.package是声明MobileSurveyApkRepo的地方。 要么 2)获取Webapplciationcontext并获取bean 为什么你要自动装配它,为什么你这样做呢? MobileSurveyApkRepo mobileSurveyApkRepo = ctx.getBean(&#34; mobileSurveyApkRepoImpl&#34;,MobileSurveyApkRepo.class);
答案 4 :(得分:0)
由于您必须进行重新安装的自动扫描,您可能需要列出您的其他服务:
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mypackage.MyRESTService</param-value>
</context-param>