Re: @Get for many Variants
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=4447dsMessageId=2440940 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2441224
Re: @Get for many Variants
Hi, You could use org.restlet.engine.converter.ConverterHelper too. You might want to register your own Converter if you're not satisfied with the default ones. In this case, the if-MediaType logic will be in the ConverterHelper's toRepresentation method. public class SearchResource extends ServerResource { @Get(xml) public SearchResults doXmlSearch() { return doSearch(getRequest()); } @Get(json) public SearchResults doJSonSearch() { return doSearch(getRequest()); } } Best wishes, Bruno. Thierry Boileau wrote: 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=4447dsMessageId=2440940 -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2441238
@Get for many Variants
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=4447dsMessageId=2440940