You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wink.apache.org by Gino Bustelo <lb...@gmail.com> on 2011/08/18 05:18:10 UTC
ExceptionMapper and Media Types
I'm looking for a way to control the rendering of an exception based on the media type. Basically, the same exception can yield an HTML payload or a JSON structure based on Accept header. I tried to achieve this with a pair of ExceptionMapper subclasses and the @Produces annotation. The json mapper is getting call all the times. What am I doing wrong? I'm think that I'm left with looking at the accept header in the mapper an producing the different response entities.
Re: ExceptionMapper and Media Types
Posted by Gino Bustelo <lb...@gmail.com>.
I actually got a nice solution to the problem. The main issue was that the wink jackson provider is not checking if response entities are asset classes. An asset class should not be handled by the jackson provider, rather let the asset provider call the right method based on media type.
I had to replace the Jackson provider with one that checks if a type has an asset annotation.
Here is the setup.
Custom exceptions
Custom exception mapper
Wrap exception in an asset class and set as response entity
Have the right media type produces annotation in the asset class methods.
Done... Right rendering of exception according to type.
Gino B.
On Aug 19, 2011, at 8:03 AM, Paulo Borges <ab...@hotmail.com> wrote:
> This may seem like an over kill but here is how I do it.
>
> I have a String instance variable in my Exception classes with default set to Media type json and before I throw the exception I set the value to the appropriate media type [if not JSON] and in my Exception Mapper the Response object uses the media type set for the exception.
>
> CC: wink-user@incubator.apache.org
> From: lbustelo@gmail.com
> Subject: Re: ExceptionMapper and Media Types
> Date: Thu, 18 Aug 2011 12:20:06 -0500
> To: wink-user@incubator.apache.org
>
> Thanks Michael... That is actually what I started doing today. Still not working. Tracing to the code and so far it seems there is something wrong picking the right produces method in the entity I'm returning. Also, I had to set the type on the response in the mapper. It just seem that winks forgets about media type when exception handling.
>
> Gino B.
>
> On Aug 18, 2011, at 9:41 AM, Michael Elman <el...@apache.org> wrote:
>
> Why do you need a pair of ExceptionMappers?
> ExceptionMapper returns Response, which can contain an entity.
> The entity will be serialized based on the Accept header.
>
> On Thu, Aug 18, 2011 at 5:24 PM, Gino Bustelo <gi...@bustelos.com> wrote:
> I'm looking for a way to control the rendering of an exception based
> on the media type. Basically, the same exception can yield an HTML
> payload or a JSON structure based on Accept header. I tried to achieve
> this with a pair of ExceptionMapper subclasses and the @Produces
> annotation. The json mapper is getting call all the times. What am I
> doing wrong? I'm think that I'm left with looking at the accept header
> in the mapper an producing the different response entities.
>
RE: ExceptionMapper and Media Types
Posted by Paulo Borges <ab...@hotmail.com>.
This may seem like an over kill but here is how I do it.
I have a String instance variable in my Exception classes with default set to Media type json and before I throw the exception I set the value to the appropriate media type [if not JSON] and in my Exception Mapper the Response object uses the media type set for the exception.
CC: wink-user@incubator.apache.org
From: lbustelo@gmail.com
Subject: Re: ExceptionMapper and Media Types
Date: Thu, 18 Aug 2011 12:20:06 -0500
To: wink-user@incubator.apache.org
Thanks Michael... That is actually what I started doing today. Still not working. Tracing to the code and so far it seems there is something wrong picking the right produces method in the entity I'm returning. Also, I had to set the type on the response in the mapper. It just seem that winks forgets about media type when exception handling.
Gino B.
On Aug 18, 2011, at 9:41 AM, Michael Elman <el...@apache.org> wrote:
Why do you need a pair of ExceptionMappers?
ExceptionMapper returns Response, which can contain an entity.
The entity will be serialized based on the Accept header.
On Thu, Aug 18, 2011 at 5:24 PM, Gino Bustelo <gi...@bustelos.com> wrote:
I'm looking for a way to control the rendering of an exception based
on the media type. Basically, the same exception can yield an HTML
payload or a JSON structure based on Accept header. I tried to achieve
this with a pair of ExceptionMapper subclasses and the @Produces
annotation. The json mapper is getting call all the times. What am I
doing wrong? I'm think that I'm left with looking at the accept header
in the mapper an producing the different response entities.
Re: ExceptionMapper and Media Types
Posted by Gino Bustelo <lb...@gmail.com>.
Thanks Michael... That is actually what I started doing today. Still not working. Tracing to the code and so far it seems there is something wrong picking the right produces method in the entity I'm returning. Also, I had to set the type on the response in the mapper. It just seem that winks forgets about media type when exception handling.
Gino B.
On Aug 18, 2011, at 9:41 AM, Michael Elman <el...@apache.org> wrote:
> Why do you need a pair of ExceptionMappers?
> ExceptionMapper returns Response, which can contain an entity.
> The entity will be serialized based on the Accept header.
>
> On Thu, Aug 18, 2011 at 5:24 PM, Gino Bustelo <gi...@bustelos.com> wrote:
> I'm looking for a way to control the rendering of an exception based
> on the media type. Basically, the same exception can yield an HTML
> payload or a JSON structure based on Accept header. I tried to achieve
> this with a pair of ExceptionMapper subclasses and the @Produces
> annotation. The json mapper is getting call all the times. What am I
> doing wrong? I'm think that I'm left with looking at the accept header
> in the mapper an producing the different response entities.
>
Re: ExceptionMapper and Media Types
Posted by Michael Elman <el...@apache.org>.
Why do you need a pair of ExceptionMappers?
ExceptionMapper returns Response, which can contain an entity.
The entity will be serialized based on the Accept header.
On Thu, Aug 18, 2011 at 5:24 PM, Gino Bustelo <gi...@bustelos.com> wrote:
> I'm looking for a way to control the rendering of an exception based
> on the media type. Basically, the same exception can yield an HTML
> payload or a JSON structure based on Accept header. I tried to achieve
> this with a pair of ExceptionMapper subclasses and the @Produces
> annotation. The json mapper is getting call all the times. What am I
> doing wrong? I'm think that I'm left with looking at the accept header
> in the mapper an producing the different response entities.
>
ExceptionMapper and Media Types
Posted by Gino Bustelo <gi...@bustelos.com>.
I'm looking for a way to control the rendering of an exception based
on the media type. Basically, the same exception can yield an HTML
payload or a JSON structure based on Accept header. I tried to achieve
this with a pair of ExceptionMapper subclasses and the @Produces
annotation. The json mapper is getting call all the times. What am I
doing wrong? I'm think that I'm left with looking at the accept header
in the mapper an producing the different response entities.