I try to build up a simple EJB-project on JBoss Wildfly.
I want a stateless EJB to be a JAX-RS resource class. This REST-service should simply return the Person-entities saved in the database.
EJB-Code:
#Stateless
#Path("/person")
public class PersonServiceBean {
#PersistenceContext EntityManager em;
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<Person> getAllPersons(){
return em.createQuery("FROM " + Person.class.getName()).getResultList();
}
}
I read I need a subclass of Application with ApplicationPath-annotation
#ApplicationPath("/rest")
public class JaxRsApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(PersonServiceBean.class));
}
}
But still I get 404 at 'localhost:8080/rest/person'.
Did I miss to configure something?
I would be really thankful for help!
The problem is that the Rest Resource must be in a WAR and not in a EJB project.
Related
I have written 2 WebSocket ServerEndpoints that inject Services that themselves interact with the Database using injected instances of the JPA EntityManager.
The application is a web application deployed on a Tomcat Server, using Jersey as JAX-RS implementation and Hibernate as JPA Provider.
Sometimes it happens that the EntityManager is closed when trying to the DB inside the Endpoints. Also I fear I might have produced code that triggers a Memory Leak.
This is the custom ServerEndpoint.Configurator that I am using (based on https://gist.github.com/facundofarias/7102f5120944c462a5f77a17f295c4d0):
public class Hk2Configurator extends ServerEndpointConfig.Configurator {
private static ServiceLocator serviceLocator;
public Hk2Configurator() {
if (serviceLocator == null) {
serviceLocator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.bind(serviceLocator, new ServicesBinder()); // binds the "normal" Services that interact with the DB
ServiceLocatorUtilities.bind(serviceLocator, new AbstractBinder() {
#Override
protected void configure() {
bindFactory(EntityManagerFactory.class).to(EntityManager.class);
}
});
}
}
#Override
public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException {
T endpointInstance = super.getEndpointInstance(endpointClass);
serviceLocator.inject(endpointInstance);
return endpointInstance;
}
}
In the rest of the application I am using the same ServicesBinder, but a different Binder for the EntityManager.
The EntityManagerFactory looks like this:
public class EntityManagerFactory implements Factory<EntityManager> {
private static final javax.persistence.EntityManagerFactory FACTORY = Persistence.createEntityManagerFactory("PersistenceUnit");
#Override
public final EntityManager provide() {
return FACTORY.createEntityManager();
}
#Override
public final void dispose(final EntityManager instance) {
instance.close();
}
}
It is loaded with the Scope RequestScoped (only there, not in the WebSocket Endpoints).
I tried creating an EntityManager instance for every access in my DAOs, but then I would run into org.hibernate.LazyInitializationExceptions eventually since my DTOs need an open EntityManager (implicitly).
Any suggestions on how to circumvent the issues I'm having?
Okay, I managed to fix my issue by simply rewriting the EntityManager handling to create and close an EntityManager every time I interact with the database.
I already looked at a lot of posts and nothing seems to work quite as i liked it to.
I want to inject an object into ContainerRequestContext properties from a filter and retrieve it later in other classes.
here is my filter:
#Priority(Priorities.AUTHENTICATION)
public class AuthorizationFilter implements ContainerRequestFilter {
#Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
containerRequestContext.setProperty("myObject", new Object());
}
}
here is the class I want access to ContainerRequestContext:
#Provider
public class SessionContextProvider implements ISessionContextProvider {
#Context
private ContainerRequestContext request;
#Override
public Object getSessionContext() {
return request.getProperty("mySessionContext");
}
}
and my spring config:
#Bean(name="sessionContextProvider")
#Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ISessionContextProvider sessionContextProvider() {
return new SessionContextProvider();
}
Everything works as expected if I inject ContainerRequestContext into my web resource. However if call my provider class ContainerRequestContext is always null.
I don't seem why this would not work.
Reagrds
Jonas
The problem is that with the Jersey/Spring integration, it allows us to successfully inject Spring beans into Jersey components, but this is not always true the other way around.
Jersey has it's own DI framework, HK21, and it is responsible for handle the injections with Jersey components. With the Jersey Spring integration, Jersey will lookup the Spring Bean, and take it as is, it won't inject it with any dependencies, I guess assuming Spring should take care of it's own injections.
That being said, if you don't require the ISessionContextProvider to be a Spring bean, then you can just make it an HK2 service. It's pretty simple. If you don't require any special initialization, you can just let HK2 create it. Here a simple configuration
public JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(new AbstractBinder() {
bind(SessionContextProvider.class)
.to(ISessionContextProvider.class)
.in(RequestScoped.class);
});
}
}
And that's it. You have an injectable ISessionContextProvider2.
If you require the ISessionContextProvider provider to be a Spring bean, then another option is to grab the bean from the Spring ApplicatinContext, and explicitly inject it yourself, using HK2's analogue of the ApplicationContext, its ServiceLocator. To do that we would need to use a Factory to do all the work transparently, so you can still inject the bean without doing any extra work on the outside
import javax.inject.Inject;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.ServiceLocator;
import org.springframework.context.ApplicationContext;
public class SessionContextProviderFactory
implements Factory<SessionContextProvider> {
private final ISessionContextProvider provider;
#Inject
public SessionContextProviderFactory(ApplicationContext ctx,
ServiceLocator locator) {
provider = ctx.getBean(ISessionContextProvider.class);
locator.inject(provider);
}
#Override
public ISessionContextProvider provide() {
return provider;
}
#Override
public void dispost(ISessionContextProvider provider) { /* noop */ }
}
Then just register the factory
public JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(new AbstractBinder() {
bindFactory(SessionContextProviderFactory.class)
.to(ISessionContextProvider.class)
.in(RequestScoped.class);
});
}
}
1 - hk2
2 - See also Dependency injection with Jersey 2.0
I found a workaround. I could inject the Spring HttpServletRequest into my AuthorizationFilter and set the SessionContext on this one.
#Autowired
private HttpServletRequest request;
....
request.setAttribute("mySessionContext", sessionContext);
And then because HttpServletRequest is known to spring and besically represents the same thing in my SessionContextProvider I do the same:
#Autowired
private HttpServletRequest request;
#Override
public SessionContext getSessionContext() {
return (SessionContext) request.getAttribute("mySessionContext");
}
I dont think this is the best solution possible. But it works. Ill wait for peeskillet for any other input if there is a better solution.
Regards
Jonas
I have a .ear that imports a .jar and a .war modules. I'm trying to inject a EJB on my war that exists in jar, and I'm getting a null pointer exception when I call it. Here's the code.
WAR PACKAGE
Controller
This property "private AutenticacaoBean authBean;" is getting a null pointer exception. I don't really know if I need to use #remote in any place, because I'm using different packages, but in the same ear.
public class LoginController extends AbstractController {
#EJB
private AutenticacaoBean authBean;
public void execute() {
//code
}
}
JAR PACKAGE
Bean
#Stateless
#LocalBean
public class AutenticacaoBean {
#EJB
private UsuarioDAO usuarioDao;
//code
}
Implementation
#Stateless
public class UsuarioDAOImpl implements UsuarioDAO {
public UsuarioDAOImpl() {
//code
}
//code
}
Interface
#Local
public interface UsuarioDAO {
//code
}
Any help would be great. Thank you for your time!
EJB 3.1 dropped the requirement for local interfaces and you are using AutenticacaoBean without the interface, but probably your Application Server is not compliant about EJB 3.1 and you cannot perform the injection in this way:
#EJB
private AutenticacaoBean authBean;
You could try to create an interface (AutenticacaoLocal for example) for your AutenticacaoBean with the needed methods and then inject this one in your LoginController:
#EJB
private AutenticacaoLocal authBean;
Exactly as you did for UsuarioDAOImpl.
I am not able to inject the cdi bean in Resteasy. While debugging it always seems to show null pointer exception. ie 'jaxRsImpl' in the below code is always null. I am trying to run on jboss eap 6.2
#Path("/jaxrs-service")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public class JAXRSService {
#Inject
private JAXRSImpl jaxRsImpl;
#POST
#Path("/authenticate")
#Consumes(MediaType.APPLICATION_JSON)
public Response authenticate(Credentials user) {
return jaxRsImpl.authenticate(user);
}
}
And the class which i intend to inject is
#RequestScoped
public class JAXRSImpl {
public Response authenticate(Credentials user) {
// Some logic
}
}
As my application is web so i have added beans.xml inside WEB-INF folder
My Initialiser looks like
#ApplicationPath("/rest")
public class JAXRSInitializer extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public JAXRSInitializer() {
singletons.add(new JAXRSService());
classes.add(JAXRSImpl.class);
}
#Override
public Set<Class<?>> getClasses() {
return classes;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
You need to ensure that your application is CDI aware. Here are some of the key requirements:
In JAX-RS, don't list out your classes/singletons. Allow the container to discover them. Basically create an empty Application implementation.
Make sure you have a valid beans.xml
Make sure your rest endpoints have a valid scope - e.g. #RequestScoped
The first bullet is key, since you're manually instantiating your service, rather than allowing the container to find them.
I am learning Java EE and JSP. I have created an Enterprise Application project in NetBeans.
I have the EJB project where all beans are and a WAR project where all web/client stuff is.
My problem is that the annotation #EJB does not instantiate my Bean in the WAR application. Can I use #EJB outside the EJB application?
In the EJB project, I have these files:
CustomerRemote.java
#Remote
public interface CustomerRemote {
public Customer createCustomer();
public Customer getCustomer(int customerId);
public void removeCustomer();
}
CustomerBean.java
#Stateless
public class CustomerBean implements CustomerRemote {
#PersistenceContext(unitName="testPU")
private EntityManager entityManager;
#Override
public Customer createCustomer() {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void removeCustomer() {
}
#Override
public Customer getCustomer(int customerId) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
In the WAR project, I have a file that my JSP page uses to communicate with the EJB stuff. The problem is that the CustomerRemote object is never instantiated. #EJB annotation does not seem to work because customerRemote always is null. But when instantiating it with the lookup method, it works! So why does not #EJB work?
public class CustomerProxyBean {
#EJB
private CustomerRemote customerRemote;
public CustomerProxyBean() {
// try {
// InitialContext context = new InitialContext();
// customerRemote = (CustomerRemote)context.lookup("java:app/SimpleShop-ejb/CustomerBean");
//
// } catch (NamingException ex) {
// Logger.getLogger(CustomerProxyBean.class.getName()).log(Level.SEVERE, null, ex);
// }
}
#EJB annotation will work only in cases where your class is container-managed, that is EJB, servlet, JSP... In your case you put it into plain old Java object (POJO) so injection will not work, as you have experienced. Write your CustomerProxyBean as a stateless session bean, and you'll see the change.
Alternatively, if you want to avoid JNDI for some reason, you can use CDI and #Inject annotation to inject EJB and achieve wished behaviour, even in POJO:
#Inject
private CustomerRemote customerRemote;