2015-11-02 38 views
3

我不能post請求RestTemplate。這表明在線RestTemplate不適用於參數(URL,請求,類別<String>)

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"); 

回答

2

您使用的是URL實例,但RestTemplate預計URI類型的對象以下錯誤。

只需更換這行:

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

這一行:

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

這應該做的伎倆。