0

starttimeオプションでとの 2 つのクエリ文字列パラメーターをサポートする REST API がありますendtime。どちらも一目瞭然です。

現在、WebRequestパラメーターをコントローラー API に渡し、タイムスタンプ ( としてエンコードされている) を検索しLong、それを に変換しCalendarます。

CalendarqueryString の処理を​​行わずに、パラメーターを自動的に API に渡す方法はあるのだろうか。何かのようなもの

public Object[] myApi([...], Calendar startTime, Calendar endTime)

最も重要なことは、パラメーター両方ともオプションである必要があることです (いずれかを指定するか、null にすることができます)。

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 に答える