Alexander Rühl
Alexander Rühl

Reputation: 6939

JAX-RS resource as POJO vs CDI vs EJB

A JAX-RS root resource is defined by a @Path annotation and might use managed components for doing the actual work, for example:

@Path("resource")
public class Resource
{
  @Inject
  Worker worker;

  @GET
  public String getDetails() {
    return worker.getDetails();
  }
}

Now I can transfer this JAX-RS root resource to either a CDI bean:

@RequestScoped
@Path("resource")
public class Resource {...}

Or to an EJB:

@Stateless
@Path("resource")
public class Resource {...}

So - what are the consequences of doing it the POJO, the CDI or the EJB way? From the outside, a request to the URL delivers three times the same thing, but what happens under the hood and how do the injected components relate to each case?

Upvotes: 3

Views: 784

Answers (1)

him
him

Reputation: 608

It pretty much comes down to context. Do you need the extra facilities that an EJB provides (well defined transaction semantics, proxied stateless pooled handlers, cluster support, etc.), or do you just need dependency injection?

Just using a CDI bean will give you that off the shelf, if that's all you need. If you don't even need that, a POJO will provide your simplest bang for the buck.

Upvotes: 3

Related Questions