I have a REST API that optionally supports two querystring parameters: starttime and endtime, both self-explanatory.
Currently I pass the WebRequest parameter to my controller API and search for the timestamp (encoded as Long), then convert it to Calendar.
I wonder if there is a way to have the Calendar parameters automatically passed to API without going with processing queryString. Something like
public Object[] myApi([...], Calendar startTime, Calendar endTime)
Most importantly, the parameters must both be optional (any can be specified or be null)
How can I do that in Spring MVC?
Example of current code:
@RequestMapping(value = "/rest/{datatype}", method = RequestMethod.GET, produces = { "application/json" })
public @ResponseBody
Object[] getData(@PathVariable("datatype") String dataType,
WebRequest request) throws HttpException {
if (dataType == null || "".equals(dataType))
throw new ClientException("Datatype cannot be empty");
Calendar timestampInit = null;
if (request.getParameter(PARAMETER_STARTTIME) != null) {
try {
timestampInit = Calendar.getInstance();
timestampInit.setTimeInMillis(Long.valueOf(request
.getParameter(PARAMETER_STARTTIME)));
} catch (NumberFormatException ex) {
throw new ClientException("Invalid start time", ex);
}
}
Calendar timestampEnd = null;
if (request.getParameter(PARAMETER_ENDTIME) != null) {
try {
timestampEnd = Calendar.getInstance();
timestampEnd.setTimeInMillis(Long.valueOf(request
.getParameter(PARAMETER_ENDTIME)));
} catch (NumberFormatException ex) {
throw new ClientException("Invalid end time", ex);
}
}
[...]
}
Take a look at StringParamUnmarshaller
http://docs.jboss.org/resteasy/2.0.0.GA/userguide/html/StringConverter.html
usually most of REST frameworks are +- same so i believe there should be something same in your configuration