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

From: Ransom Briggs <>
Date: Tue, 6 Sep 2011 15:14:37 -0500


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.

@Produces({"application/json", "text/html"})
public class FooResource {
    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.