0

我有一个 REST API,可以选择支持两个查询字符串参数:starttimeendtime,都是不言自明的。

目前我将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);
        }
    }
            [...]
        }
4

1 回答 1

0

看看 StringParamUnmarshaller http://docs.jboss.org/resteasy/2.0.0.GA/userguide/html/StringConverter.html 通常大多数 REST 框架都是 +- 相同,所以我相信你的配置应该有相同的东西

于 2013-02-12T13:48:36.240 回答