我在 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);
}
如何解决?
将“http”方案添加到您的网址,
如果您使用的是 localhost,请像这样传递 url - http://localhost:8080/api/notes 定义方案很重要(http/https) 也不使用空间。 对于您的问题,答案应该是- http://localhost:8086/addMessage
将 http:// 或 https:// 方案添加到 URL 对我有用
restTemplate.postForObject("http://localhost:8086/addMessage", orders, String.class);
或
restTemplate.postForObject("https://localhost:8086/addMessage", orders, String.class);
谢谢@stacker 和@Shubham