Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTeasy, EJBs and @Context object injection

I have been developing a small Java EE application with Glassfish. It's basically a RESTful services application. It was a web application with the RESTful services developed as EJBs. The whole thing was created using the Netbeans wizards and everything worked perfectly on Glassfish 3.1.1.

I tried to deploy the application on JBoss 7.1.1 and the problems started. I tried to use Jersey and everything worked but the Persistence Unit was not properly injected in the EJBs. I converted the application to a full blown Java EE application contained normally in an ear (war, ejb-jar, ear) and configured RESTEasy in the web.xml to work with EJBs. Now everything works but using the @Context to inject instances of javax.ws.rs.core.HttpHeaders, javax.ws.rs.core.Request etc is not working. The fields are always null.

It is frustrating that in each step of the way I would find a problem with RESTeasy.

This is a cut-down version of my web.xml:

<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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>resteasy.jndi.resources</param-name>
        <param-value>java:module/CountryFacadeREST</param-value>
    </context-param>
    <context-param>
      <param-name>resteasy.servlet.mapping.prefix</param-name>
      <param-value>/webresources</param-value>
   </context-param>
    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>
    <servlet>
        <servlet-name>RESTeasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RESTeasy</servlet-name>
        <url-pattern>/webresources/*</url-pattern>
    </servlet-mapping>
</web-app>

This is a sample EJB:

package com.mt.rest.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import com.mt.entity.Country;

/**
 * 
 * @author mt
 */
@Stateless
@Path("country")
public class CountryFacadeREST extends AbstractFacade<Country> {
    @PersistenceContext(unitName = "myparkPU")
    private EntityManager em;

    @Context
    HttpHeaders requestHeaders;

    @Context
    Request request;

    public CountryFacadeREST() {
        super(Country.class);
    }

    @POST
    @Override
    @Consumes({ "application/json" })
    public void create(Country entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({ "application/json" })
    public void edit(Country entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({ "application/json" })
    public Country find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Produces({ "application/json" })
    public List<Country> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({ "application/json" })
    public List<Country> findRange(@PathParam("from") Integer from,
            @PathParam("to") Integer to) {
        return super.findRange(new int[] { from, to });
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}

I followed the following advice from RESTeasy documentation but nothing has changed:

3.6. Patching JBoss AS 7 Resteasy is bundled with JBoss AS 7. You will likely have the need to upgrade Resteasy in AS7. The Resteasy distribution comes with a zip file called resteasy-jboss-modules-2.3.4.Final.zip. In the top level directory of JBoss AS 7 (the one with the modules/ directory), unzip this file. You should be patched with the latest and greatest Resteasy after this.

I have supplied an empty web.xml and created an application class:

package com.mt.rest.service;

import javax.ws.rs.ApplicationPath; 
import javax.ws.rs.core.Application;

@ApplicationPath("/webresources") 
public class ApplicationConfig extends Application { }

but still the @Context objects injection fails. Note that the Persistence Unit is properly injected.

like image 675
Timmo Avatar asked Mar 12 '26 21:03

Timmo


1 Answers

OK it seems that the @Context objects do not get injected if there is a @Stateless annotation:

This is the actual bug which is open

So a solution would be to define the RESTful service as a POJO and inject the EJB with the @EJB annotation which does not work. In order for it to work you must define your service as an EJB or as a managed CDI managed bean using the @Named annotation.

Here is the actual answer on the JBoss forum

Finally it works...

like image 129
Timmo Avatar answered Mar 16 '26 06:03

Timmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!