我一直在使用 Glassfish 开发一个小型 Java EE 应用程序。它基本上是一个 RESTful 服务应用程序。它是一个 Web 应用程序,具有作为 EJB 开发的 RESTful 服务。整个东西是使用 Netbeans 向导创建的,一切都在 Glassfish 3.1.1 上完美运行。
我尝试在 JBoss 7.1.1 上部署应用程序,但问题开始了。我尝试使用 Jersey,一切正常,但持久性单元未正确注入(inject) EJB。我将该应用程序转换为通常包含在 ear(war、ejb-jar、ear)中的完整 Java EE 应用程序,并在 web.xml 中配置 RESTEasy 以使用 EJB。现在一切正常,但使用 @Context 注入(inject) javax.ws.rs.core.HttpHeaders 的实例, javax.ws.rs.core.Request 等不工作。这些字段始终为空。
令人沮丧的是,在这个过程中的每一步我都会发现 RESTeasy 的问题。
这是我的 web.xml 的简化版本:
<web-app version="3.0" 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_3_0.xsd">
<context-param>
<param-name>resteasy.jndi.resources</param-name>
<param-value>java:module/CountryFacadeREST</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/webresources</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>RESTeasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RESTeasy</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
</web-app>
这是一个示例 EJB:
package com.mt.rest.service;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import com.mt.entity.Country;
/**
*
* @author mt
*/
@Stateless
@Path("country")
public class CountryFacadeREST extends AbstractFacade<Country> {
@PersistenceContext(unitName = "myparkPU")
private EntityManager em;
@Context
HttpHeaders requestHeaders;
@Context
Request request;
public CountryFacadeREST() {
super(Country.class);
}
@POST
@Override
@Consumes({ "application/json" })
public void create(Country entity) {
super.create(entity);
}
@PUT
@Override
@Consumes({ "application/json" })
public void edit(Country entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
@Produces({ "application/json" })
public Country find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Produces({ "application/json" })
public List<Country> findAll() {
return super.findAll();
}
@GET
@Path("{from}/{to}")
@Produces({ "application/json" })
public List<Country> findRange(@PathParam("from") Integer from,
@PathParam("to") Integer to) {
return super.findRange(new int[] { from, to });
}
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return String.valueOf(super.count());
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
我遵循了 RESTeasy 文档中的以下建议,但没有任何改变:
3.6. Patching JBoss AS 7 Resteasy is bundled with JBoss AS 7. You will likely have the need to upgrade Resteasy in AS7. The Resteasy distribution comes with a zip file called resteasy-jboss-modules-2.3.4.Final.zip. In the top level directory of JBoss AS 7 (the one with the modules/ directory), unzip this file. You should be patched with the latest and greatest Resteasy after this.
我提供了一个空的 web.xml 并创建了一个应用程序类:
package com.mt.rest.service;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/webresources")
public class ApplicationConfig extends Application { }
但@Context 对象注入(inject)仍然失败。请注意持久性单元已正确注入(inject)。
最佳答案
好的,如果有 @Stateless 注释,@Context 对象似乎不会被注入(inject):
This is the actual bug which is open
因此,解决方案是将 RESTful 服务定义为 POJO,并使用不起作用的 @EJB 注释注入(inject) EJB。为了使其工作,您必须使用 @Named 批注将您的服务定义为 EJB 或托管 CDI 托管 bean。
Here is the actual answer on the JBoss forum
终于成功了...
关于jboss7.x - RESTeasy、EJB 和@Context 对象注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12821328/