RestTemplate不适用于参数(URL,Request,Class <string>)

时间:2015-11-02 09:21:09

标签: java spring-mvc resttemplate

我无法post RestTemplate的请求。它在线上显示以下错误

The method postForObject(URI, Object, Class<T>) in the type RestTemplate is 
not applicable for the arguments (URL, Request, 
 Class<String>)

代码

        URL url = new URL("http://testnl.etbxml.com/api");
        Authentication auth = new Authentication("Test", "test");
        auth.setFunction("SearchAvailability");

        Request req = new Request("test");
        req.setAuth(auth);
        req.setCityid(23);
        req.setStartdate("2015-11-20");
        req.setEnddate("2015-11-29");
        req.setRating(4);
        req.setNoofpersons(2);
        req.setLanguage("en");
        req.setCurrency("EUR");
        req.setCustomerIP(MY_IP);
        req.setAuth(auth);

        RestTemplate restTemplate = new RestTemplate();
        //restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
        //restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
        //String response = restTemplate.postForObject(url, req, EasyToBook.class);
        //Line 21
        Easytobook ea = restTemplate.postForObject(url, req, String.class);

        auth.setFunction("SearchAvailability");

1 个答案:

答案 0 :(得分:2)

您使用的是URL个实例,但RestTemplate需要URI类型的对象。

只需替换此行:

URL url = new URL("http://testnl.etbxml.com/api");

这一行:

URI url = new URI("http://testnl.etbxml.com/api");

这应该可以解决问题。