将Bean类注入Restfull WebService(JAX-RS) [英] Cant inject Bean class into Restfull WebService (JAX-RS)
问题描述
我正在尝试使用Hibernate /持久性将Rest Web服务获取的数据保存到数据库。
在我的一个Web模块中,我实现了这个服务。数据库ejb连接器放置在EJB模块中。它们是EAR应用的一部分。
每次当我调用pb.addDevice()时,在浏览器中使用params放置正确的url(工作,直到我想要保存到数据库)时,获取java.lang.NullPointerException。找不到有什么问题。我使用jboss 6.1.0 Final。
尝试回答依赖注入在休息的WS
之后,一步一步地,我得到nullpointer也
PS。当我从
@EJB
PersistenceBean pb;
至
PersistenceBean pb = new PersistenceBean();
我在EntityManager上得到空指针em = emf.createEntityManager();
代码:
@Stateless
@Path / RestService)
public class RestPush {
@EJB
PersistenceBean pb;
@GET
@Path(/ RegisterDevice)
public void registerDevice(
@QueryParam(deviceId)String deviceId){
设备d =新设备(true);
d.setId = deviceId;
pb.addDevice(d);
}
}
和EJB类:
$ b $public class PersistenceBean {
@PersistenceUnit(unitName =PersistentUnitName)
EntityManagerFactory emf
private void persist(Object o,EntityManager entityManager){
try {
entityManager.persist(o);
} catch(Exception e){
logger.severe(写入DB时出错:+ e);
logger.severe(+ e.fillInStackTrace());
}
}
public void addDevice(Device d){
try {
EntityManager em = emf.createEntityManager();
if(persist(device,em)){
logger.info(Device with id:+ device.getId()
+已添加);
} else {
logger.info(无法添加设备ID:+ device.getId());
} catch(Exception e){
logger.severe(PersistenceBean:无法保存设备);
e.printStackTrace();
}
}
upadate:
EAR
--EarContent
--META-INF
--application.xml
EJB
- ejbModule中的包装
--PersistenceBean
--Device
--META-INF
--ejb-jar.xml
- MANIFEST .MF
--persistence.xml
--beans.xml
Web
- webModule中的包
--Rest(自动生成类while创建Webservice)
--RestPush
--WebContent
--META-INF
--MANIFEST.MF
--WEB-INF
- web.xml
--beans.xml
堆栈跟踪:
`10:23:28,629 ERROR [org.apache.catalina.core.ContainerBase。[jboss.web]。[localhost]。[/ RestWeb] 。[Resteasy]] Servlet.service()for servlet Resteasy抛出异常:org.jboss.resteasy.spi.UnhandledException:java.lang.NullPointerException
at
引起的:java.lang.NullPointerException
在packag e.RestPush.registerDevice(RestPush.java:68)[:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.6.0_27]
at sun.reflect.NativeMethodAccessorImpl.invoke( NativeMethodAccessorImpl.java:57)[:1.6.0_27]
@EJB
注释不适用于所有对象。
为了使您能够使用CDI,请将 @EJB
替换为 @Inject
,您的bean将被正确注入。
编辑:
还要确保将$ code> beans.xml 添加到包含要注入的类的每个jar / war存档中注射它进入 META-INF
for jars, WEB-INF
作为战争。
您的REST应用程序类 packaget.Rest
应该扩展 javax.ws.rs.core.Application
如:
@ApplicationPath(/ root-path)
public class Rest extends Application
{
}
根据文档这里应该开箱即用。如果您指定了org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher和org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap,那么您可能会搞砸RestEasy / CDI类加载。
所以你的 web.xml
应该看起来如下:
< web-app version =3.0xmlns =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 java.sun.com/xml/ns/javaee/...\">
< / web-app>
无论如何,我在 github
I'm trying to save data acquired by Rest web service to database using hibernate/persistence. In one of my web modules i implemented that service. Database ejb connector is placed in EJB module. They are parts of EAR application. Every time when i call pb.addDevice() im getting java.lang.NullPointerException when puting proper url with params in browser(worked till i wanted to save it to Database). Can't find what is wrong with it. I'm using jboss 6.1.0 Final.
tried answer of Dependency injection in restful WS
and after following it step by step im alse getting nullpointer also
PS. when i changed from
@EJB
PersistenceBean pb;
to
PersistenceBean pb = new PersistenceBean();
i got null pointer on EntityManager em = emf.createEntityManager();
code:
@Stateless
@Path("/RestService")
public class RestPush {
@EJB
PersistenceBean pb;
@GET
@Path("/RegisterDevice")
public void registerDevice(
@QueryParam("deviceId") String deviceId){
Device d = new Device(true);
d.setId = deviceId;
pb.addDevice(d);
}
}
and EJB class:
@Stateless(mappedName = "PersistenceBean")
public class PersistenceBean {
@PersistenceUnit(unitName = "PersistentUnitName")
EntityManagerFactory emf;
private void persist(Object o, EntityManager entityManager) {
try {
entityManager.persist(o);
} catch (Exception e) {
logger.severe("Error writing to DB: " + e);
logger.severe("" + e.fillInStackTrace());
}
}
public void addDevice(Device d) {
try {
EntityManager em = emf.createEntityManager();
if (persist(device, em)) {
logger.info("Device with id : " + device.getId()
+ " has been added ");
} else {
logger.info("Failed to add device with id: " + device.getId());
} catch (Exception e) {
logger.severe("PersistenceBean: Could not save device.");
e.printStackTrace();
}
}
upadate:
EAR
--EarContent
--META-INF
--application.xml
EJB
--package in ejbModule
--PersistenceBean
--Device
--META-INF
--ejb-jar.xml
--MANIFEST.MF
--persistence.xml
--beans.xml
Web
--package in webModule
--Rest (auto generated class while creating Webservice)
--RestPush
--WebContent
--META-INF
--MANIFEST.MF
--WEB-INF
--web.xml
--beans.xml
stack trace:
`10:23:28,629 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/RestWeb].[Resteasy]] Servlet.service() for servlet Resteasy threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at
Caused by: java.lang.NullPointerException
at package.RestPush.registerDevice(RestPush.java:68) [:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_27]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [:1.6.0_27]
The @EJB
annotation is not supposed to work for all objects.
For this to work you need to use CDI, so substitute the @EJB
with @Inject
and your bean will be correctly injected.
See also: Inject an EJB into JAX-RS (RESTful service)
EDIT:
Also be sure to add beans.xml
to every jar/war archive containing classes you want to inject or be injected. It goes into META-INF
for jars and WEB-INF
for wars.
Your REST application class packaget.Rest
should extend javax.ws.rs.core.Application
as in:
@ApplicationPath("/root-path")
public class Rest extends Application
{
}
And according to the documentation here on JBoss 6.1 REST and CDI should work out of the box. If you specify the org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher and the org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap you are probably messing up the RestEasy/CDI classloading.
So your web.xml
should look as:
<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 java.sun.com/xml/ns/javaee/…">
</web-app>
Anyway, I pushed a working example on github
这篇关于将Bean类注入Restfull WebService(JAX-RS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!