LuckyLuke
LuckyLuke

Reputation: 49097

Where is it usual to apply JAX-RS annotations?

I have read a little on JAX-RS and I wonder where it is usual to put the annotations? Should I add them on my existing EJBs? Or should I make new EJBs, and if so, should I use the interfaces (if any) that I use on the "regular" EJBs?

Upvotes: 2

Views: 172

Answers (1)

Arjan Tijms
Arjan Tijms

Reputation: 38163

As you found out, there are multiple ways how to model them. I'm not sure of the overall best practice, but I personally choose to use CDI managed beans that I inject with EJB beans.

So, that's a bit like your second option, make new beans, but don't make them EJBs too.

E.g.

@Produces("application/xml")
@Path("xml")
@javax.enterprise.context.RequestScoped // CDI one, not JSF one
public class MyResource {

    @Context
    private SecurityContext securityContext;

    @Inject
    private MyDAO myDAO; // MyDAO = EJB Bean

    @GET
    @Path("some/path")
    public Foo getFoo() {
        return myDAO.getFooByUserName(securityContext.getUserPrincipal().getName());
    }
}

It also depends if there are methods that do things that is only used for JAX-RS. The above example doesn't show this, but I found it often creeps up and when it does separate beans are just a bit neater.

Upvotes: 2

Related Questions