Java Spring RestTemplate:使用可为null的参数获取

Java Spring RestTemplate:使用可为null的参数获取,java,spring,rest,spring-mvc,resttemplate,Java,Spring,Rest,Spring Mvc,Resttemplate,我正试图提供真正的RestFull服务,并遵守文档。然而,我现在遇到了一个我看不到明确答案的问题。我想使用一个过滤器从Web服务查询一些数据。以下路径是在Web服务的控制器上定义的 @RequestMapping(value="/rest/postalcode/list/filter?lang={lang}&postalcode={postalcode}&country={country}&city={city}", method = RequestMethod.GET,

我正试图提供真正的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,例如,因为用户没有填写要筛选的postalcode。国家和城市也是如此(lang总是填写)。但每次运行它时,我都会发现一个IOException未找到(可能是由于null的原因)。我试过一次,所有的东西都填好了,我在服务端的方法非常完美。那么你如何处理这样的问题呢

我可以通过把GET扔出窗口来解决这个问题,把所有的东西都放在一个帖子主体中,作为一个JSONobject映射到Jackson上,问题就解决了。但是我使用POST来获取数据,而在纯REST中应该使用GET来获取数据


那么RestTemplate和查询具有可变数据的服务如何进行呢?

只是洗了个冷水澡,然后自己发现了:)

我不必使用pathvariables,我只需要使用请求参数

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

你能在这里发布异常的完整堆栈跟踪吗?这可能会对我们的建议有所帮助。但是我自己发现了,我仍然在学习关于休息的各种知识。在我以前的工作中,我只使用肥皂。