Hello,

you can override the "ServerResource#get(Variant)" method (which is 
historically the fundation of the Resource's behaviour, with the post, 
put, etc methods):

     @Override
     protected void doInit() throws ResourceException {
         // Declare the list of supported variants.
         getVariants().add(new Variant(MediaType.APPLICATION_XML));
         getVariants().add(new Variant(MediaType.APPLICATION_JSON));
     }

     @Override
     protected Representation get(Variant variant) throws 
ResourceException {
         Representation result = null;

         MediaType type = variant.getMediaType();
         if (MediaType.APPLICATION_XML.equals(type)) {
             result =new 
XmlSearchResponseRepresentation(doSearch(getRequest()));
          } else if (MediaType.APPLICATION_JSON.equals(type)) {
             result = new 
JSonSearchResponseRepresentation(doSearch(getRequest()));         }

         return result;
     }

Best regards,
Thierry Boileau

> Hi everyone,
>
> I'm using restlet to write a Search service..
> This service will respond in XML or JSon..
>
> Actually I have a code like that :
> == 8<  ======================================
> public class SearchResource extends ServerResource {
>
>    @Get("xml")
>    public Representation doXmlSearch(Variant v) throw ResourceException {
>      return new XmlSearchResponseRepresentation(doSearch(getRequest()));
>    }
>
>    @Get("json")
>    public Representation doJSonSearch(Variant v) throw ResourceException {
>      return new JSonSearchResponseRepresentation(doSearch(getRequest()));
>    }
>
> }
> == 8<  ======================================
>
> As you can see, the unique difference is the Representation name. So I wonder 
> if it is a way to write only one method that make a "if" on the requested 
> variant to create an Xml or JSon representation..
>
> Thanks
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2440940
>
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441224

Reply via email to