Reputation: 1801
I have a service in Java using Jersey
, now I want to map following three URLs to a single method, so that if any function have .json
or .xml
convert the output accordingly if no extension (format) is provided then default return is json
Following code work fine but if do not specify .xml or .json url is not found
/api/getData (result is json)
/api/getData.json (result is json)
/api/getData.xml (result is xml)
Please note I cannot change it to
/api/getData/xml
/api/getData/json
I want them to be functionName.format
@POST
@Path("/getData{format}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getData(
@FormParam("token") String token,
@PathParam("format") String format,
@Context HttpServletRequest context) {
....
}
Upvotes: 1
Views: 936
Reputation: 1579
You need to add an wildcard sign to support non-extension requests with a single resource, like:
{[pathParamName]:([allowedEndings])[wildcardSign]}
-> {ext:(.json|.xml)*}
Example:
@GET
@Path("foo/{bar}{ext:(.json|.xml)*}")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public synchronized Response getEvents(@PathParam("bar") String bar, @PathParam("ext") String ext) {
if("".equals(ext))
ext = ".json";
System.out.println(bar);
System.out.println(ext);
// ...
return null;
}
Have a nice day ...
Upvotes: 2