[Jersey] Re: How would I implement a messagebodywriter that uses templates based on resource name?

From: Martin Matula <>
Date: Wed, 07 Sep 2011 16:16:28 +0200

Hi Ransom,
You can still use Viewables, just not the implicit ones. You can write a
code like this:

public class FooResource {
     public List<FooDto> index() {
         FooDto foo = new FooDto();
         List<FooDto> foos = new ArrayList<FooDto>();
         return foos;

     public Viewable indexHtml() {
         return new Viewable("index", index());


On 6.9.2011 22:14, Ransom Briggs wrote:
> Hello,
> After fighting with ImplicitViewables for a morning, I'm finding that
> they are not really working for my use case. What I am trying to
> accomplish is serving up JSON and HTML from the same action. I am
> trying to keep the resources simple, so I have something like this
> right now, where actions return the entity that should be served as
> both JSON and HTML.
> @Path("/foo")
> @Produces({"application/json", "text/html"})
> public class FooResource {
> @GET
> public List<FooDto> index() {
> FooDto foo = new FooDto();
> List<FooDto> foos = new ArrayList<FooDto>();
> return foos;
> }
> }
> I was going to write a MessageBodyWriter that could serialize to HTML,
> but I could not figure out how to access the resource instance, which
> I need so I could use templates to defined different HTML views for
> different actions. Ideally, my code would result in something like
> ImplicitViewables where the template would be found in
> FooResource/index.jsp, except instead of ${it} being the resource, it
> would the entity that the method returned.
> Thanks,
> Ransom