users@jersey.java.net

[Jersey] Re: Trying to render JSON from a Viewable based on Accept Headers.

From: Bill O'Neil <oneil5045_at_gmail.com>
Date: Sun, 9 Feb 2014 13:14:07 -0500

Sorry accidentally hit send before finishing the email. Here is what I
have.

@GET
@Path("/test")
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_HTML})
@Template(name="/static/templates/mytemplate")
public Object metrics() {
return new Object();
}

I am also using a sub class of UriConnegFilter so that I can just append
.json to the end of a url and have the filter convert it to the correct
Accept Header (accept=[application/json]).

I have been debugging through for quite a while and it seems like there is
no way to return false for isWriteable in the ViewableMessageBodyWriter
class.

Here is the implementation.
@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations,
                           final MediaType mediaType) {
    return Viewable.class.isAssignableFrom(type);
}

Is there a way I can tell the template to only render for certain media
types? Ideally configureable.
@Override
public boolean isWriteable(final Class<?> type, final Type genericType,
final Annotation[] annotations,
                           final MediaType mediaType) {
    return isAcceptableMediaType(mediaType) &&
Viewable.class.isAssignableFrom(type);
}


Or is there another way to approach this? Currently I am using the
Mustache templates and it will render for both Accept: application/json as
well as Accept: text/html as HTML. However it does correctly assign the
Media type for the response. In the case of Accept: application/json, it
is returning HTML with the media type of application/json.

I was hoping I could just override the isWriteable method but most of the
classes in the MVC package are package private.

Please let me know if I am missing a step to make this work.
Thanks, Bill

On Sun, Feb 9, 2014 at 1:00 PM, Bill O'Neil <oneil5045_at_gmail.com> wrote:
>
> I am trying to use the same method to return JSON as well as an HTML view
of the JSON.
>
>
>