Hi,
I've just started to work with Restlet and I would like to ask for
comments/guidance on the best way to implement
a server that responds in several formats (JSON and JSONP for now).
I'm using GAE.
I've done a quick test by adapting the FirstStepsApplication example
and got it working but I'm not sure this is the
best way.
What I would like to do is to use a converter for JSONP just like for
JSON so that I would have to explicitly do
the convertion for the JSONP case.
Is this possible? I'm guessing it wouldn't be a regular converter
because it must accept an external parameter (the callback name).
How could this be done? I've checked the source for the
JacksonConverter but honestly I didn't understand much of it.
Can anyone point me to some examples that do JSONP or to documentation
on how to implement a converter?
thanks,
jorge
----------
package org.jorgecardoso.resttest.server;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.data.Metadata;
import org.restlet.routing.Router;
public class FirstStepsApplication extends Application {
@Override
public Restlet createInboundRoot() {
// Create a router Restlet that routes each call to a
// new instance of HelloWorldResource.
Router router = new Router(getContext());
router.attach("/rest/", HelloWorldResource.class);
this.getTunnelService().setExtensionsTunnel(true);
this.getTunnelService().setMediaTypeParameter("output");
this.getMetadataService().addExtension("jsonp",
MediaType.APPLICATION_JAVASCRIPT, true);
return router;
}
}
------------------
and my HelloWorldResource is:
-----------------
package org.jorgecardoso.resttest.server;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.restlet.ext.jackson.*;
public class HelloWorldResource extends ServerResource {
Contact c;
String callback = "call";
@Override
public void doInit() {
c = new Contact("jorge cardoso", 30);
callback = this.getQuery().getFirstValue("callback",
"defaultCallback");
}
@Get("jsonp")
public String representJsonp() {
JacksonRepresentation jr = new JacksonRepresentation( c );
String s = "";
try {
s = callback + "(" + jr.getText() + ")";
} catch (Exception e) {
e.printStackTrace();
}
return s;
}
@Get("json")
public Contact representJson() {
return c;
}
}
-----------------
a contact is just:
------------------
public class Contact {
public String name;
public int age;
public int height;
public Contact(String name, int age) {
this.name = name;
this.age = age;
height = 10;
}
}
--------------
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2710187