users@glassfish.java.net

[gf-users] Re: [gf-issues] [JAVA EE7] JAX-RS/EJB annotation problem with JAVA 8 interface methods

From: Reza Rahman <reza.rahman_at_oracle.com>
Date: Wed, 4 Feb 2015 16:04:12 -0500

More than likely these are not scenarios that have been tested and implemented against. In fact I am not sure if the spec even handles it. I would file bugs.

> On Feb 4, 2015, at 3:36 PM, Paulo Reis <casmeiron_at_gmail.com> wrote:
>
> Hi,
>
> We’re tryin’ few java 8 features with JAVA EE 7 using glassfish v4.1.
>
> JAX-RS is not able to work with @Path annotation when used in a method implementation inside an interface.
> Example:
>
> public interface Controller {
>
> @GET
> default Response ok() {// it works
> return Response.ok.build();
> }
>
> @GET
> @Path(“error”)
> default Response error() { // doesn’t work
> return Response.ok.build();
> }
>
> @Produces({APPLICATION_JSON})
> @Consumes({APPLICATION_JSON})
> @LocalBean
> @Stateless
> public class ControllerImplementation implements Controller {
>
> }
>
> When you try to run with the second method uncommented, JAX-RS throws exception when it tries to initialize the REST stack. Weird ‘cause @GET/_at_POST works, only @Path gives error.
>
> Another weird situation happens when you try to annotate your interface methods with @TransactionAttribute annotation. EJB is unable to check that info and open a transaction, thus giving an error (TransactionRequired).
>
> To fix that, you have to override the method in your implementation class and call the super, and then the transaction gets opened.
>
> Example:
>
> public interface Controller {
>
> @POST
> @TransactionAttribute(TransactionAttributeType.MANDATORY)
> default Response create(JsonObject obj) {
> return Response.ok.build();
> }
>
> @Produces({APPLICATION_JSON})
> @Consumes({APPLICATION_JSON})
> public class ControllerImplementation implements Controller {
> // you must override to make the transaction available, otherwise it will fail!
> @Override
> @POST
> public Response create(JsonObject obj) {
> super.create(obj);
> }
> }
>
>
> So it seems we cannot use all features that java 8 brings without having headache with JAVA EE 7. Does anybody knows why this happens?
>
> Thanks.