【问题标题】:RestTemplate, Spring boot , POSTRestTemplate、Spring 启动、POST
【发布时间】:2018-12-15 11:04:42
【问题描述】:

我在 localhost:8086/addMessage 有一个 rest api,当我使用 POSTMAN 测试它时它可以工作。但是当我想在客户端集成这个 api 时,它会返回:

java.net.URISyntaxException: Expected scheme-specific part at index 10: localhost:
    at java.net.URI$Parser.fail(URI.java:2848) ~[na:1.8.0_171]  error:

这是我调用api的方法:

  public void addOrder(Message orders) throws  Exception
    {  RestTemplate restTemplate = new RestTemplate();
      String resp = restTemplate.postForObject(
                "localhost:8086/addMessage",
                orders,
                String.class);    
    }

我该如何解决。

【问题讨论】:

    标签: spring api post httpclient


    【解决方案1】:

    将“http”方案添加到您的网址,

    http://localhost:8086/addMessage

    【讨论】:

      【解决方案2】:

      如果你使用 localhost 然后像这样传递 url - http://localhost:8080/api/notes 定义方案很重要(http/https) 也不要使用空间。 对于您的问题,答案应该是- http://localhost:8086/addMessage

      【讨论】:

        【解决方案3】:

        http://https:// 方案添加到 URL 对我有用

        restTemplate.postForObject("http://localhost:8086/addMessage", orders, String.class); 
        

        restTemplate.postForObject("https://localhost:8086/addMessage", orders, String.class);  
        

        感谢@stacker 和@Shubham

        【讨论】:

          猜你喜欢
          • 2021-03-22
          • 2021-06-19
          • 2011-11-26
          • 1970-01-01
          • 1970-01-01
          • 2020-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多