RestTemplate、Spring boot、POST

问题描述 投票:0回答:3

我在 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);    
    }

如何解决?

java spring post
3个回答
30
投票

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

http://localhost:8086/addMessage


0
投票

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


0
投票

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

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

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

谢谢@stacker 和@Shubham

© www.soinside.com 2019 - 2024. All rights reserved.