users@glassfish.java.net

[gf-users] Injecting JAX-RS Client into an EJB

From: Lachezar Dobrev <l.dobrev_at_gmail.com>
Date: Wed, 10 Sep 2014 11:24:50 +0300

  Hello all,

  I've been revolving around Springframework for the last 10 years,
but seeing much desired progress with J2EE 7 I am trying to pull
myself into clean J2EE and see if that works.

  For my current project I have to consume a remote HTTP service. I
decided that JAX-RS will be the perfect match for that.

  The service expects a request with
Post(application/x-www-form-urlencoded) and the response is a XML with
incorrect content-type of text/html .

  I have a set of questions that are not strictly Glassfish specific,
but more generic J2EE, but I hope I can get help here.

  1. How do I get the WebClient reference.
  Currently I am creating my own in @PostConstruct and close()-ing it
in @PreDestroy, but I feel as if I'm doing it wrong. Isn't there a
@Inject or @Resource to instruct the container to set up a WebClient
and inject it. I believe that would be much better resource-wise,
since it would allow the container to manage such heavy-weight
resources.

  2. How do you deal with invoking services that incorrectly return
wrong Content-Type?
  The service I need to connect to is not under my control, and no
changes can be applied to it.
  Currently I insert a ReaderInterceptor when creating a WebTarget,
that changes the response Content-Type.
  Is this solution acceptable?
  Is this solution the correct one?

  3. Am I doing this right?
  Maybe I am missing something, and instead of manually creating a
Client and WebTarget and setting everything up I need to declare a
remote service that the container will connect to on my behalf? Like
defining a Service Interface and declaring an @Inject or @Resource
field that will be filled with a proxy that calls the remote service
(I think something like this exists for JAX-WS).

  Let me stress, that I am trying to *CONSUME* a remote service in an
EJB, not provide one.

  Lachezar.