NullPointerException when I try to inject EJB in JAX-RS - jboss

I have seen all the issues noticed in this forum, but I still don't have any solution for my problem.
I work on a EAR project with Maven, Wildfly and JBoss.
When I try to browse to my resource I have NPE.
This is my JAX activator class:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/resource")
public class JaxRsActivator extends Application{
}
and this is my resource class:
#Path("/drivers")
public class DriverResource {
#Inject DriverCpoServiceLocal driverCpoServiceLocal;
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<DriverCpo> doFindAllDrivers(){
return driverCpoServiceLocal.findAll();
}
}
This code works correctly on my personal computer, but in the work computer I see the NPE.
It is driving me crazy.

Related

#ApplicationScoped Bean Not Eagerly Instantiated on Wildfly 16

I have an #ApplicationScoped bean that I want to be instantiated on application startup (see sample code below). I've registered for the servlet context initialization event but the init method is never invoked. The FactoryLocator is contained in a jar within my war's WEB-INF/lib directory. This same code was working on Wildfly 9 but no longer works after upgrading to Wildfly 16.
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Initialized;
import javax.enterprise.event.Observes;
import javax.servlet.ServletContext;
#ApplicationScoped
public class FactoryLocator {
public FactoryLocator() {
System.out.println("In the constructor.........................");
}
private void init(#Observes #Initialized(ApplicationScoped.class) ServletContext sc) {
System.out.println("Invoking the event observer method..................");
}
}
One workaround I discovered was to extract the contents of the jars in my war's WEB-INF/lib/ directory to my war's WEB-INF/classes directory. When I did this my FactorLocator bean was instantiated successfully. I'd prefer not to have to do this.
After debugging through the war's deployment, I noticed that the ServletContext is initialized before all the beans in the war's lib directory are processed (org.jboss.weld.bootstrap.BeanDeploymentModules:processBeanDeployments). The war's module is not aware of all observers until all the beans are processed. However, the ServletContextEvent is fired before all the observers are known.
Should this happen that ServletContext is created before all observers are known about? I've debugged one of the Wildfly QuickStart applications and the ServletContext isn't created until all bean deployments are processed and the code below is executed successfully. Is the order in which this happens supposed to be guaranteed?
Just in case anyone else has seen the same issue. Upgrading to Wildfly 21.0.1.Final resolved the issue for me.

JPA #Inheritance strategy failure after domain deported to a JAR

I had to change a JPA Inheritance strategy from SINGLE_TABLE to JOINED after moving my entities to an Maven module, in order for them to used from different applications.
The junit tests were OK when launched within Eclipse (Spring Boot, Spring Data JPA) but failed at Maven build (Mapping Exception).
#Entity
//#Inheritance(strategy = TABLE_PER_CLASS)
//#DiscriminatorColumn(name = "privilege_datarealm_type")
#Inheritance(strategy = JOINED)
public abstract class PrivilegeDataRealm<E extends SchemaElement>
extends Privilege<PrivilegeDataRealm<E>>
implements SchemaElement {
Does anyone have any clue of explanations for this issue ?

stateless annotation cannot be found

I'm having a little problem using JPA (Hibernate)
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
/**
* Bean decribing a user.
*
*/
#Entity
public class User implements Serializable {
...
I'm able to use some of the annotation (like #Entity), as shown in the example above.
But #Stateless cannot be found by Eclipse.
Why ?
I'm using the latest version from Hibernate (4.1.9)
I created a user library containing all the required library from the hibernate directory.
I upgraded my dynamic web project with the JPA Project Facet.
I solved my problem by using TomEE web server, which contains EJB implementation
#Stateless annotation are related to EJB stuffs.For this you need to include EJB3.1 jar in your project build path.

GlassFish Error: JAX-RS EJB support is disabled

I am trying a simple app using Jersey JAX-RS + EJB 3.1 on GlassFish 3.1.2.2. All seemed to look pretty well with Jersey REST on GlassFish until I added EJB. When deploying the war file, I got this error.
SEVERE: Error when configuring to use the EJB interceptor binding API. JAX-RS EJB support is disabled.
Anyone who has encountered this before? Is there a configuration in GlassFish to fix this?
My EJB is a simple pojo with #Singleton and #PostConstruct annotation.
#Singleton
public class PurchaseBean {
private String name;
#PostConstruct
public void init() {
System.out.println("Initializing PurchaseBean");
setName("Purchase Singleton EJB");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Looks like this is an Eclipse issue. I did the deployment using Eclipse with the GlassFish adapter. Restarting Eclipse solved it.
Solution: Shutdown GlassFish, inside Eclipse go to Project->Clean(select project) and then start GlassFish again.
I don't know why but it worked. :)
Working solution or the ones who run the Glassfish standalone: Restart the Glassfish. Glassfish version number 4.1.2

Google Guice 3 and OSGi (Eclipse equinox) problem,

I have trouble running Guice 3 within an OSGi container.
Following is a simple test I wrote to test if Guice work well with OSGi.
A simple guice module like:
public class Module extends AbstractModule {
#Override
protected void configure() {
bind(IInterface.class).to(IImplement.class);
}
}
The IInterface and IImplement are both very trivial.
The OSGi activator like this:
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
Injector inj = Guice.createInjector(new Module());
IInterface e = inj.getInstance(IInterface.class);
e.sayHello();
}
In Eclipse, I made a target contains all the Guice Jars, and to make guice resolve itself, I made two additional bundle for the aopalliance.jar and javax.injector.jar
However, this simple test fail to load the test bundle, gives me error message complaining cannot find a guice class cannot be found:
Exception in guicetest.Activator.start() of bundle guicetest
Caused by: java.lang.NoClassDefFoundError: com/google/inject/binder/AnnotatedBindingBuilder
at guicetest.guice.Module.configure(Module.java:11)
I hope I have made the problem clear. Can anyone show me how to resolve this problem?
Ah, after just posting the question I found the root of the problem. I didn't specify the com.google.inject.binder package, which the problematical class resides, in the test bundle's Import-Packages. Although the Module doesn't import directly that package, it looks it is still necessary to specify all the indirect dependent packages as well.