users@glassfish.java.net

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

From: Reza Rahman <reza.rahman_at_oracle.com>
Date: Wed, 10 Sep 2014 09:18:05 -0400

Firstly a warm welcome back to standard Java :-). You'll get the best answers to your questions on the Jersey/JAX-RS forums, where most of the Java EE REST experts lurk! Let me know if you need directions to get there but it should probably be pretty straightforward.

> On Sep 10, 2014, at 4:24 AM, Lachezar Dobrev <l.dobrev_at_gmail.com> wrote:
>
> 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.