users@jersey.java.net

Re: [Jersey] ExceptionMapper and constructor injection of _at_Context UriInfo ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 23 Jul 2009 15:02:13 +0200

On Jul 23, 2009, at 2:14 PM, Morten wrote:

> I would like to provide some great context specific errors to jersey
> 1.1.1 so I have added an ExceptionMapper like the code extract below
> which access UriInfo using CTR injection.
>
> Unfortunately, jersey gives an error that the class can't be
> instantiated (assumingly because it does not support injection of
> the provider).

Any more information on the error? stack trace?

Many of the internal providers supplied by Jersey use constructor
injection. I did a quick test and it works for me e.g. i modified the
helloworld sample [1] resource class to be:

   // The Java class will be hosted at the URI path "/helloworld"
   @Path("/helloworld")
   public class HelloWorldResource {

       @Provider
       public static class MyExceptionMapper implements
ExceptionMapper<Throwable> {

           public MyExceptionMapper(@Context UriInfo uriInfox) {
           }

           public Response toResponse(Throwable exception) {
               return Response.ok("Hello World").build();
           }
       }

       // The Java method will process HTTP GET requests
       @GET
       // The Java method will produce content identified by the MIME
Media
       // type "text/plain"
       @Produces("text/plain")
       public String getClichedMessage() {
           throw new UnsupportedOperationException();
   // // Return some cliched textual content
   // return "Hello World";
       }
   }

Paul.

[1] http://download.java.net/maven/2/com/sun/jersey/samples/helloworld/1.1.1-ea/helloworld-1.1.1-ea-project.zip

> Is this a bug in jersey 1.1.1 or a know limitation ?
>
> @Provider
> public class CustomExceptionMapper implements
> ExceptionMapper<java.lang.Throwable>
> {
> public CustomExceptionMapper(@Context UriInfo uriInfox)
> {
> // Save uri for late (note this req. class is NOT a singleton).
> }
>
> public Response toResponse(java.lang.Throwable ex)
> {
> // Detailed response here.
> }
> }
>
> /Cheers,
> Morten
>
>
>
> Trænger du til at se det store billede? Kelkoo giver dig gode
> tilbud på LCD TV! Se her http://dk.yahoo.com/r/pat/lcd
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>