0
我有一个REST API,可以选择支持两个查询字符串参数:starttime
和endtime
,这两个参数都是不言自明的。来自REST API中可选QueryString参数的日历对象
目前我将WebRequest
参数传递给我的控制器API并搜索时间戳(编码为Long
),然后将其转换为Calendar
。
我不知道是否有一种方法可以将Calendar
参数自动传递给API,而无需处理queryString。喜欢的东西
public Object[] myApi([...], Calendar startTime, Calendar endTime)
最重要的是,参数必须两个是可选的(任何可以指定或为空)
我如何能做到这一点在Spring MVC?当前代码
例子:
@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);
}
}
[...]
}