0
votes

I have an app that runs fine on jboss7, it uses jee6 apis: jax-rs and CDI. Due to production limitations I was asked to adapt it to run on Jboss 5.1.0. To make things easy I created a minimal application, with only one service and one injection. It works fine on jb7, and I had to add the libs that are not provided for the jboss5 version.

jax-rs worked fine, I added RestEasy and declared the RestEasy servlet on the web.xml.

For CDI I included Weld, to work on servlet mode, since the application does not have servlets. Following the docs, I included the listener on the web.xml, and then found on a jboss forum that another listener is necessary to register the BeanContext to the jboss jndi.

So what I have is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 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_2_5.xsd">


    <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener> 

    <listener>
        <listener-class>org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener</listener-class>
    </listener> 

    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>br.com.bvmf.services.ApplicationConfig</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>    

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>  
</web-app>

For the build I am using Gadle, but just to have a better understanding for not I am inserting most of the jars manually for now:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

webAppDirName = 'WebContent'

repositories {
    mavenCentral()
    flatDir name:'EasyRestJars',dirs:'libs'
}


dependencies {
    compile(

        [name : 'weld-servlet', version : '2.0.0.SP1-jboss5'],

        [name : 'jaxrs-api', version : '2.3.6.Final'],
        [name : 'jackson-core-asl', version : '1.9.9'],
        [name : 'jackson-jaxrs', version : '1.9.9'],
        [name : 'jackson-mapper-asl', version : '1.9.9'],
        [name : 'resteasy-jackson-provider', version : '2.3.6.Final'],
        [name : 'resteasy-jaxb-provider', version : '2.3.6.Final'],
        [name : 'resteasy-jaxrs', version : '2.3.6.Final'],
        [name : 'scannotation', version : '1.0.3']
    )

    providedCompile(
        [group: 'org.slf4j', name: 'slf4j-api', version: '1.5.6'],
        [group: 'javaee',    name: 'javaee-api', version: '5' ]
    )

    testCompile group: 'junit', name: 'junit', version: '4.+'

}


test {
    systemProperties 'property': 'value'
}

war{
    exclude 'WEB-INF/lib/*'
}

Now the three classes:

Tha application config just returns the service:

@ApplicationPath("min")
public class ApplicationConfig extends Application {

     @Override
        public Set<Class<?>> getClasses() {
         Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
            resources.add(br.com.bvmf.services.HelloWorldResource.class);
            return resources;
        }

}

The bean that should be injected:

public class HelloProvider {    
    public String getString() {     
        return "Injected Hello";
    }
}

And the service itself:

@Path("/helloworld")
public class HelloWorldResource {

    @Inject
    private HelloProvider provider;

    @GET
    public String getMessage() {
        //This is where I get a null pointer
        return provider.getString();

    }
}

So the bean is not injected, just that. During the deploy, the log reads:

2013-06-04 15:24:31,371 INFO  [org.jboss.webbeans.bootstrap.WebBeansBootstrap] (HDScanner) Web Beans 1.0.0.PREVIEW1
2013-06-04 15:24:32,898 INFO  [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (HDScanner) deploy, ctxPath=/min-cil-1.0-SNAPSHOT
2013-06-04 15:24:33,417 INFO  [org.jboss.weld.Version] (HDScanner) WELD-000900 2.0.0 (SP1)
2013-06-04 15:24:33,774 INFO  [org.jboss.weld.Bootstrap] (HDScanner) WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
2013-06-04 15:24:34,485 WARN  [org.jboss.weld.Bootstrap] (HDScanner) Legacy deployment metadata provided by the integrator. Certain functionality will not be available.
2013-06-04 15:24:34,809 INFO  [org.jboss.weld.environment.tomcat.Tomcat6Container] (HDScanner) Tomcat 6 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
2013-06-04 15:24:35,750 INFO  [org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener] (HDScanner) BeanManager reference bound to java:comp/env/BeanManager
2013-06-04 15:26:13,648 INFO  [org.jboss.resteasy.spi.ResteasyDeployment] (http-127.0.0.1-8080-2) Deploying javax.ws.rs.core.Application: class br.com.bvmf.services.ApplicationConfig

There is a note in the weld docs saying that: "Additionally, Weld Servlet supports JBoss EAP 5.1, to do this use the jboss5 variant of Weld Servlet." So I found this version, but it made no difference (besides more problems with slf4j).

2
Any luck on the forums? - LightGuard
No, it seems Weld is not supported on JBoss 5.1 afterall. In the end I changed the dependency injection to Spring with annotations and autowire. Spring played well with RESTEasy, so its all fine now. - ciczan

2 Answers

0
votes

I don't think there is any integration with JAX-RS in Weld Servlet - this is only available in Java EE 6. You could look at the way it is done in JBoss AS 6 and try to backport it perhaps? Or look at the CDI and RESTEasy docs to work out how to add it (basically, you need to register some sort of post-construct listener with RESTEasy, and then use CDI APIs to inject the instance).

0
votes

It seems Weld is not supported on JBoss 5.1 afterall. In the end I changed the dependency injection to Spring with annotations and autowire. Spring played well with RESTEasy, so its all fine now.