【问题标题】:RestTemplate is not applicable for the arguments (URL, Request,Class<String>)RestTemplate 不适用于参数(URL、Request、Class<String>)
【发布时间】:2015-11-02 09:21:09
【问题描述】:

我不能通过RestTemplatepost 提出请求。它在线显示以下错误

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

【问题讨论】:

    标签: java spring-mvc resttemplate


    【解决方案1】:

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

    只需替换这一行:

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

    用这一行:

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

    这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多