users@jersey.java.net

[Jersey] Re: Putting simple web interface to jersey REST endpoint

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 20 Dec 2011 16:25:07 +0100

Hi Farrukh,

I think you want to utilize the ImplicitViewables feature of Jersey.
There you do not need to explicitly return Viewable instances from your
resource methods.
See the Bookstore ([1]) example for details. This example utilizes JSP,
but it should work
in a similar way with FreeMarker.

~Jakub

[1]http://search.maven.org/remotecontent?filepath=com/sun/jersey/samples/bookstore/1.11/bookstore-1.11-project.zip

On 19.12.2011 18:46, Farrukh Najmi wrote:
>
> It seems that com.sun.jersey.freemarker.FreemarkerViewProcessor
> (jersey-freemarker contrib module)may have a role to play. But again I
> am not sure how to create or lookup an instance of this class. Thanks
> for any help on this.
>
> On 12/19/2011 12:40 PM, Farrukh Najmi wrote:
>>
>> I think I see how I need to proceed but I have some questions inline
>> below....
>>
>> Here is what I think I need to do:
>>
>> * Define a MessageBodyWriter for the response type returned by by
>> resource methods like below...
>>
>>
>> @Produces("text/html")
>> @Provider
>> public class HTMLMessageBodyWriter implements
>> MessageBodyWriter<MyResponseType> {
>>
>> public boolean isWriteable(Class<?> type, Type type1,
>> Annotation[] antns, MediaType mt) {
>> return MyResponseType.class.isAssignableFrom(type);
>> }
>>
>> public long getSize(MyResponseType t, Class<?> type, Type
>> type1, Annotation[] antns, MediaType mt) {
>> return -1;
>> }
>>
>> public void writeTo(MyResponseType response, Class<?> type,
>> Type type1, Annotation[] antns, MediaType mt, MultivaluedMap<String,
>> Object> mm, OutputStream out) throws IOException,
>> WebApplicationException {
>> Writer writer = new OutputStreamWriter(out);
>> HTMLFormatter htmlFormatter = new HTMLMarshaller();
>> htmlFormatter.marshall( response, writer);
>> writer.close();
>> out.flush();
>> }
>>
>> }
>>
>> * Define an HTMLFormatter class to marshall the response to
>> custom formatted HTML
>>
>> * Use jersey-freemarker to use freemarker templates to generate
>> the HTML as needed
>>
>>
>> What I cannot determine is how to best get the freemarker template
>> within my HTMLFormatter class. Does the
>> com.sun.jersey.api.view.Viewable
>> <http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/view/Viewable.html>class
>> have a role to play? Can any one tell me if I am on the right track
>> and how bets to get the freemarker template? Thanks.
>>
>>
>>
>> On 12/19/2011 09:13 AM, Farrukh Najmi wrote:
>>> Hi ManiKanta,
>>>
>>> Thank you for introducing me to the Viewable
>>> <http://jersey.java.net/nonav/apidocs/1.1.0-ea/jersey/com/sun/jersey/api/view/Viewable.html>
>>> class in jersey. It will be useful in my solution. However, your
>>> post does not seem to address the need I have as follows:
>>>
>>> * Keep a single set of resource methods that return XML response
>>> * Allow XML response to be algorithmically mapped to HTML using
>>> some custom mapping class(es)
>>>
>>> I had mentioned that my current resource methods somehow support
>>> "application/json" automatically using the
>>> com.sun.jersey:jersey-json:jar:1.10:compile dependency even though
>>> the method just returns an XML response. Clearly the jersey-json
>>> adds support for marshalling/unmarshalling json from XML. I want to
>>> do similar by marshalling responses to HTML.
>>>
>>> I am just not familiar enough with jersey API and framework as to
>>> how to do that. Given more time I will figure it out but I was
>>> hoping that someone could provide high level steps of how to do
>>> that. Thank in advance.
>>>
>>> On 12/19/2011 02:02 AM, ManiKanta G wrote:
>>>> you could do that using Viewable
>>>> <http://jersey.java.net/nonav/apidocs/1.1.0-ea/jersey/com/sun/jersey/api/view/Viewable.html> we
>>>> are serving the html response for html mime type by having a
>>>> generic resource method in super class
>>>>
>>>> see
>>>> http://blog.docuverse.com/2009/08/04/using-jsp-with-jersey-jax-rs-implementation/
>>>> http://stackoverflow.com/questions/3361877/jersey-viewable-with-status-code
>>>>
>>>>
>>>> ManiKanta G
>>>> twitter.com/ManiKantaG <http://twitter.com/ManiKantaG>
>>>>
>>>>
>>>> On Sat, Dec 17, 2011 at 11:39 PM, Farrukh Najmi
>>>> <farrukh_at_wellfleetsoftware.com
>>>> <mailto:farrukh_at_wellfleetsoftware.com>> wrote:
>>>>
>>>> Hi Guys,
>>>>
>>>> My jersey based REST endpoint serves resources in their native
>>>> XML format when default or in JSON format when application/json
>>>> mediatype is specified. Things are working pretty well.
>>>>
>>>> I would like to add a simple Web interface for human access to
>>>> my REST endpoint and was thinking that I could add support for
>>>> mediatype text/html in my endpoints resource methods via @Produces.
>>>>
>>>> What I am not sure of what is the best way to do this in
>>>> jersey. Jersey support JSON format without any help from my
>>>> code so I assume there is a global filter somewhere to
>>>> transform XML to JSON. I am looking to do similar with HTML.
>>>> Thanks for any guidance on this.
>>>>
>>>
>
> --
> Regards,
> Farrukh Najmi
>
> Web:http://www.wellfleetsoftware.com
>