0

私は真の RestFull サービスを作ろうとしており、ドキュメントを守っています。しかし、私は今、明確な答えが見えない問題に悩まされています。フィルターを使用して、Web サービスから一部のデータを照会したいと考えています。次のパスは、Web サービスのコントローラーで定義されます。

@RequestMapping(value="/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}", method = RequestMethod.GET, produces="application/json")
public @ResponseBody JsonPostalCodeList findFilteredPostalCodes(@PathVariable("lang") String lang, @PathVariable("postalcode") String postalcode, @PathVariable("country") Long country, @PathVariable("city") String city, Model model) throws Exception {
}

次に、クライアント側で次のメソッドで呼び出してみます

public JsonPostalCodeList findPostalCodes(
        JsonPostalCodeSelectorData selectorData) {
    String url = getWebserviceLocation()+"/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}";
    MbaLog.debugLog(logger,"Calling webservice with url: " + url);
    return getRestTemplate().getForObject(url, JsonPostalCodeList.class, selectorData.getContactLanguage(), selectorData.getPostalCode(), selectorData.getCountry(), selectorData.getCity());       
}

現在、selectorData.getPostalCode() は null になる可能性があります。たとえば、ユーザーがフィルタリングする郵便番号を入力していないためです。国と都市についても同様です (lang は常に入力されます)。しかし、実行するたびに、IOException not found が発生します (おそらく null が原因です)。すべてを入力して一度試してみましたが、サービス側で自分の方法で完璧に進みました。では、このような問題をどのように処理しますか?

ウィンドウから GET をスローし、Jackson でマップされた JSONobject としてすべてを POST 本文に入れるだけで解決でき、問題は解決します。しかし、私は POST を使用してデータをフェッチしていますが、純粋な REST では GET を使用してデータをフェッチする必要があります。

では、RestTemplate と変数データを使用したサービスのクエリは、どのように行うのでしょうか?

4

1 に答える 1

2

冷たいシャワーを浴びて、自分でそれを見つけました:)

パス変数を使用する必要はありません。要求パラメーターを使用するだけです。

@RequestMapping(value="/rest/postalcode/list/filter", method = RequestMethod.GET, produces="application/json")
public @ResponseBody JsonPostalCodeList findFilteredPostalCodes(@RequestParam("lang") String lang, @RequestParam("postalcode") String postalcode, @RequestParam("country") Long country, @RequestParam("city") String city, Model model) throws Exception {
}

そしてそれを呼び出す

@Override
public JsonPostalCodeList findPostalCodes(
        JsonPostalCodeSelectorData selectorData) {
    String url = getWebserviceLocation()+"/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}";
    MbaLog.debugLog(logger,"Calling webservice with url: " + url);
    return getRestTemplate().getForObject(url, JsonPostalCodeList.class, selectorData.getContactLanguage(), selectorData.getPostalCode(), selectorData.getCountry(), selectorData.getCity());       
}
于 2012-08-25T19:54:10.293 に答える