users@jersey.java.net

[Jersey] JAX-RS Providers and CDI RequestScoped beans

From: Jan Algermissen <algermissen1971_at_mac.com>
Date: Sun, 29 Apr 2012 14:21:59 +0200

Hi,

I plan to inject JAX-RS providers into a request scoped bean in JavaEE6. This raises the question of the scope that JAX-RS Providers have in this case. I looked at the JAX-RS spec and if I understand it correctly, the container should instantiate new (per request) provider instances when they are injected into a request scoped bean.

Is that correct?

IOW, is it save to do the following? (And will CDI actually see the MBRs)?

@RequestScoped (or alternatively @Stateless)
public class MyService {

  @Inject
  Interface<MessageBodyReader> messageBodyReaders;

  public void doStuff() {
    for (MessageBodyReader mbr : messageBodyReaders) {
      if(mbr.isReadable( .... ) {
         mbr.readFrom( ... );
        return;
      }
      // handle no MBR found case.
    }
  }