Should the REST URL include a /jsonp if I have to implement JSONP using Jersey?
Here is my Jersey code for the method
@GET
@Path("/dates/jsonp")
@Produces("application/javascript")
public JSONWithPadding getCompetitionsByDate(
@QueryParam("dateFrom") String dateFrom,
@QueryParam("dateTo") String dateTo,
@QueryParam("callback") String callback) {
DateFormat df = new SimpleDateFormat("MMddyyyy");
Date dateF = null;
Date dateT = null;
try {
dateF = df.parse(dateFrom);
dateT = df.parse(dateTo);
} catch (ParseException parseExp) {
return null;
}
List<Competition> competitions = context.getCompetitions(
dateF, dateT);
GenericEntity<List<Competition>> compGenericEntity = new GenericEntity<List<Competition>>(competitions) {};
JSONWithPadding padding = new JSONWithPadding(compGenericEntity,
callback);
return padding;
}
When I deploy and load the URL on the browser, this returns the correct JSON padded with the callback method, but when I change the path to @Path("/dates?enable=jsonp"), it doesn’t work and returns an XML representation of the the competitions.
I am using JSON 1.10 and the app is deployed on Glassfish 3.1.1.
q: Should the REST URL include a /jsonp if I have to implement JSONP using Jersey?
a: not necessarily (it can be included, but doesn’t need to be).
your other issue is that you are putting query params into @Path value, which is not possible; see http://jersey.java.net/nonav/apidocs/latest/jersey/javax/ws/rs/Path.html.
you can have something like this:
and differentiate between these to by setting proper “accept” header in your request.