1 Reply Latest reply on Jul 30, 2013 11:07 AM by jfuerth

    Custom @Path mapping for Errai Jax-rs

    xybrek

      Is it possible to have custom @Path annotation for Jax-rs methods like this:

       

      @Path("service")

      public interface StuffService {

          @POST

          @Path("/create") // This one

          @Consumes(MediaType.APPLICATION_JSON)

          @Produces(MediaType.TEXT_PLAIN)

          public String createSomething(Something stuff);

        

          @GET

           @Path("/{hash}") // This one

          @Consumes(MediaType.APPLICATION_JSON)

          @Produces(MediaType.APPLICATION_JSON)  

          public Stuff getSomethingByHash(String hash);

      }

       

      With Errai Jax-RS? My point is I want to make sure that Restful service created for Errai is also available for access for non-Errai clients.

       

      In a typical Jax-rs interface it will look like this:

       

          @POST

          @Path("/create")

          @Consumes(MediaType.APPLICATION_JSON)

          @Produces(MediaType.TEXT_PLAIN)

          public Response createSomething(@Form Stuff stuff, @Context HttpHeaders headers, @Context UriInfo uriInfo,

          @Context SecurityContext securityContext);

       

      and

       

          @GET

          @Path("/{hash}")

          @Produces(MediaType.TEXT_HTML)

          public Response getSomethingByHash(@PathParam("hash") String hashId, @Context HttpHeaders headers,

          @Context UriInfo uriInfo, @Conteext SecurityContext securityContext);

       

      Annotating a Errai model with like @FormParam("stuff_id") is not a problem, so I guess this idea is possible. The question is how to mix or combine these two kind of interface such that we get the best of both worlds. That is, I have this standard Jax-rs interface, callable from other non-Errai client while making Errai client side call this method as easy like Errai-Jax-rs in the client-side.

       

      Also, I think the response cannot be a POJO and must be a "Response" object to be able to be usable with non-Errai clients. If this is so, then how would a typical Errai client side call would look like?

        • 1. Re: Custom @Path mapping for Errai Jax-rs
          jfuerth

          Xybrek Xybrek wrote:

           

          Is it possible to have custom @Path annotation for Jax-rs methods like this:

           

          @Path("service")

          public interface StuffService {

              @POST

              @Path("/create") // This one

              @Consumes(MediaType.APPLICATION_JSON)

              @Produces(MediaType.TEXT_PLAIN)

              public String createSomething(Something stuff);

            

              @GET

               @Path("/{hash}") // This one

              @Consumes(MediaType.APPLICATION_JSON)

              @Produces(MediaType.APPLICATION_JSON)  

              public Stuff getSomethingByHash(String hash);

          }

           

          With Errai Jax-RS? My point is I want to make sure that Restful service created for Errai is also available for access for non-Errai clients.

           

           

          Yes, both of the above should work with Errai's JAX-RS client. The Something type will have to be marshalable by Errai Marshalling.

           

          In a typical Jax-rs interface it will look like this:

           

              @POST

              @Path("/create")

              @Consumes(MediaType.APPLICATION_JSON)

              @Produces(MediaType.TEXT_PLAIN)

              public Response createSomething(@Form Stuff stuff, @Context HttpHeaders headers, @Context UriInfo uriInfo,

              @Context SecurityContext securityContext);

           

          and

           

              @GET

              @Path("/{hash}")

              @Produces(MediaType.TEXT_HTML)

              public Response getSomethingByHash(@PathParam("hash") String hashId, @Context HttpHeaders headers,

              @Context UriInfo uriInfo, @Conteext SecurityContext securityContext);

           

           

           

          The @Context parameters will be awkward for you when you're calling these methods from the client. To make the client-side Caller<> code cleaner, I'd recommend you use fields for the @Context variables you want JAX-RS to inject.

           

          Annotating a Errai model with like @FormParam("stuff_id") is not a problem, so I guess this idea is possible. The question is how to mix or combine these two kind of interface such that we get the best of both worlds. That is, I have this standard Jax-rs interface, callable from other non-Errai client while making Errai client side call this method as easy like Errai-Jax-rs in the client-side.

           

          Also, I think the response cannot be a POJO and must be a "Response" object to be able to be usable with non-Errai clients. If this is so, then how would a typical Errai client side call would look like?

           

          By default, this JSON is in Errai's own marshalling format. Errai includes a JAX-RS provider that handles the marshalling of request and response objects on the server side, so that the client and server are speaking the same dialect. Just make sure errai-jaxrs-provider.jar is deployed to the server.

           

          Alternatively, you can choose to use Jackson marshalling on the server. If you do this, you have to configure the client to expect Jackson-flavoured JSON rather than ErraiMarshalling-flavoured JSON. The Errai Reference Guide explains how: https://docs.jboss.org/author/display/ERRAI/Errai+JAX-RS#ErraiJAX-RS-EnablingJacksonmarshalling -- note that in this case, you must not deploy errai-jaxrs-provider.jar with your app.

           

          When you use Jackson marshalling, interop with non-Errai clients should be straightforward.

           

          -Jonathan