Spring RestTemplate : get with nullable paramaeter

I'm trying to make a true RestFull service and keeping to the documentation. However I'm stuck now with a problem I can't see a clear answer for. I want to use a filter to query some data from the webservice. The following path is defined on the controller of the webservice

@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 {
}

Then I try to call it with the following method on client side

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());       
}

now selectorData.getPostalCode() can be null for example because , the user didn't fill in a postalcode to filter on. Same can be true for country and city (lang is always filled in). But each time I run it I get an IOException not found (probably due to the null's). I tried once with everything filled in and I go perfectly in my method at service side. So how do you handle such a problem ?

I can solve it by throwing GET out of the window and just put everything in a POST body as a JSONobject mapped with Jackson and problem solved. But then I'm using a POST to fetch data while a GET should be used in pure REST to fetch data.

So RestTemplate and querying services with variable data how to go about it ?

Answers


Just took a cold shower and found it out myself :)

I don't have to use pathvariables I can just use request parameters.

@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 {
}

and calling it with

@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());       
}

Need Your Help

Opening div on selection of radio with picture

javascript jquery html css

The first 3 options in the slider are working correctly, but the others aren't working. The following is the HTML code:

How can I get a JavaScript stack trace of an error in Firefox?

javascript firefox runtime-error stack-trace

I've searched all other questions and they tell me to use .trace()

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.