Inject an EJB into JAX-RS (RESTful service)
I am not sure this is supposed to work. So either:
Option 1: Use the injection provider SPI
Implement a provider that will do the lookup and inject the EJB. See:
- @EJB injection.
Example for com.sun.jersey:jersey-server:1.17 :
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Type;
/**
* JAX-RS EJB Injection provider.
*/
@Provider
public class EJBProvider implements InjectableProvider<EJB, Type> {
public ComponentScope getScope() {
return ComponentScope.Singleton;
}
public Injectable getInjectable(ComponentContext cc, EJB ejb, Type t) {
if (!(t instanceof Class)) return null;
try {
Class c = (Class)t;
Context ic = new InitialContext();
final Object o = ic.lookup(c.getName());
return new Injectable<Object>() {
public Object getValue() {
return o;
}
};
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Option 2: Make the BookResource an EJB
@Stateless
@Path("book")
public class BookResource {
@EJB
private BookEJB bookEJB;
//...
}
See:
- How to Combine REST Services with EJB 3.1
- EJB 3.1 And REST - The Lightweight Hybrid
Option 3: Use CDI
@Path("book")
@RequestScoped
public class BookResource {
@Inject
private BookEJB bookEJB;
//...
}
See:
- Injecting an EJB from a jar into a jax-rs class in a war
Interface EJB bean is not injected in JAX-RS Implementation class?
ImplementationRest EJB bean is instaniated using javax.ws.rs.core.Application instead EJB Container.
public class RestServiceInvoke1 extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(ImplementationRest.class);
return classes;
}}
This is the reason EJB Bean classes not injected in to anohter Bean that instantiated via javax.ws.rs.core.Application. Afterwards i achieved this implementation by Spring Framework like ejb-local-ref in web.xml , jee:jndi-lookup in bean.xml. Please go through with IBM Support URLs.
Go through below link for Summarizing this issue.
http://wedowebsphere.de/blogpost/accessing-jpa-jax-rs-service-directly
Cant inject Bean class into Restfull WebService (JAX-RS)
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
JAVA-EE7/javax.ws.rs: Injection of EJB in REST-Resource
Try this:
@ApplicationPath("/rest")
public class RestConfiguration extends Application {
private Set<Class<?>> resources = new HashSet<Class<?>>();
public RestConfiguration () {
resources.add(RegistrationRest.class);
}
@Override
public Set<Class<?>> getClasses() {
return resources;
}
}
Make sure your web.xml has this:
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
Finally, make sure you have beans.xml in your WEB-INF folder
<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
EJB injection into Restful service (NullPointerException)
Declare the restful class with @Stateless, too. Here is an example: Inject an EJB into JAX-RS (RESTful service)
Related Topics
Redirect to an External Url from Controller Action in Spring MVC
Encrypt Password in Configuration Files
How Does Auto Boxing/Unboxing Work in Java
Why to Use Interfaces, Multiple Inheritance VS Interfaces, Benefits of Interfaces
Simpledateformat Parsing Date with 'Z' Literal
Why Would One Mark Local Variables and Method Parameters as "Final" in Java
Java:If a Extends B and B Extends Object, Is That Multiple Inheritance
Android.View.Inflateexception Error Inflating Class Android.Webkit.Webview
Split String on Spaces in Java, Except If Between Quotes (I.E. Treat \"Hello World\" as One Token)
Java Simpledateformat("Yyyy-Mm-Dd'T'Hh:Mm:Ss'Z'") Gives Timezone as Ist
How to Change Text Color in the Jtextarea
How to Count Occurrences with Groupby