0

I have some problem finding out how get JAX-RS 2.0 to work with CDI on wildfly 10. I got some answer on another post that was a mix of JAX-RS 1.0/2.0 and they used other dependencies than the included libraries in Wildfly.

My objective is to inject a singleton enterprise bean that encapsulate business logic that resides in the same jar into my REST resource. The REST resource class is supposed to be request scoped and only deal with REST functionality (request and response). My POJO classes are JAXB notated.

How can I use JAX-RS 2.0 with the include CDI libraries in Wildfly 10?

The bean interface

@Local
public interface DateBean {
    Date getLocalFormatDate();
}

The bean

@Singleton
public class DateBeanImpl implements DateBeanLocal {

    private static final Logger LOG = Logger.getLogger("org.test.logger");

    public DateBean() {
        LOG.fine("DateBean");
    }

    @Override
    public Date getLocalFormatDate() {
        Calendar cal = Calendar.getInstance();
        TimeZone localZone = TimeZone.getDefault();
        cal.setTimeZone(localZone);
        Date localTime = cal.getTime();
        return localTime;
    }
}

The REST resource

@Path("classroom")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class ClassRoomResource {

    private static final Logger LOG = Logger.getLogger("org.clearbyte.logger");
    @Inject private DateBean dateBean;

    public ClassRoomResource() {
        LOG.fine("ClassRoomResource");
    }

    @GET
    @Path("{id}/getDummy")
    public ClassRoom getDummy(@PathParam("id") long id) {
        ClassRoom room = new ClassRoom();
        room.setRoomName("Dummy");
        room.setRoomNr(id);
        return room;
    }

    @GET
    @Path("localDate")
    @Produces({MediaType.TEXT_HTML})
    public Response getLocalformatDate() {
        LOG.fine("DateBean variable: " +dateBean);
        Date localDate = dateBean.getLocalDate();
        LOG.fine("Local date: " +localDate);
        return Response.status(Response.Status.OK)
                .entity(localDate.toString())
                .build();
    }        
}
Community
  • 1
  • 1
Chris
  • 712
  • 3
  • 16
  • 39
  • I don't get the point of this question. JAX-RS supports CDI out of the box. – cassiomolin May 19 '16 at 10:06
  • I don't get your point too. To enable a JAX-RS resource in a Java EE 7 container you only have to use JAX-RS annotations (depending on Java EE spec for sample in your maven dependencies), and then to inject whatever you want to inject (session beans, managed beans, ...) using CDI. No need to depend on Resteasy libs too (except if you uses Resteasy specific features) – Rémi Bantos May 19 '16 at 11:55
  • True, no need to add the dependencies for JAX-RS 2.0. Edited the answer. – Chris May 19 '16 at 15:58

1 Answers1

0

The Resteasy implementation of JAX-RS 2.0 are included in Wildlfy 10, so there is no need to add further dependencies.

The interface doesn’t need @Localwhen is resides in the same jar/war for CDI to find it. To make the enterprise bean singleton in CDI use the @ApplicationScope, you can omit the @Singleton notation if you don't need a managed bean with read/write synchronisation.

@ApplicationScoped
public class DateBeanImpl implements DateBean {

    private static final Logger LOG = Logger.getLogger("org.test.logger");

    public DateBean() {
        LOG.fine("DateBean");
    }

    @Override
    public Date getLocalFormatDate() {
        //DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        TimeZone localZone = TimeZone.getDefault();
        cal.setTimeZone(localZone);
        Date localTime = cal.getTime();
        return localTime;
    }
}

The make the REST resource request scoped use the @RequestScoped notation. Notice that the @Inject inject the interface and not the implementation of the bean.

@RequestScoped
@Path("classroom")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class ClassRoomResource {

    private static final Logger LOG = Logger.getLogger("org.clearbyte.logger");
    @Inject private DateBean dateBean;
    ...

No configuration of the web.xml is necessary if you a extend the jax-rs Application class.

@ApplicationPath("rest")
public class ClassRoomApp extends Application {

private final Set<Class<?>> resources = new HashSet<>();

    public ClassRoomApp() {
        resources.add(ClassRoomResource.class);
    }

    @Override
    public Set<Class<?>> getClasses() {
        return resources;
    }
}
Chris
  • 712
  • 3
  • 16
  • 39