I am currently using Jersey to return JSON. How do I return JSONP instead? For example, my current RESTful method is:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Order> getOrders() {
return OrderRepository.getOrders();
}
I would like to deploy on Tomcat (don't want to require the use of GlassFish). My only dependencies on Jersey are:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9.1</version>
</dependency>
What additional dependencies are needed? How will the code change?
Thanks.
Naresh
Take a look at the JSONP example (especially the ChangeList resource) in Jersey (you can download the whole project here).
What you basically need to do is something like this:
@GET
@Produces(MediaType.APPLICATION_JSON)
public JSONWithPadding getOrders() {
return new JSONWithPadding(new GenericEntity<List<Order>>(OrderRepository.getOrders()){});
}
It's important to modify the correct answer, as @s_t_e_v_e says. It should be something like this:
@GET
@Produces({"application/javascript"})
public JSONWithPadding getOrdersJSonP(@QueryParam("callback") String callback) {
List<Order> orderList = OrderRepository.getOrders();
return new JSONWithPadding(new GenericEntity<List<Order>>(orderList){},
callback);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With