简体   繁体   中英

RestTemplate, Spring boot , POST

I have a rest api at localhost:8086/addMessage and it works when I test it using POSTMAN. But when I want to integrate this api on client side it returns:

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:

This is my method that calls api:

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

How can I solve it.

Add 'http' scheme to your url,

http://localhost:8086/addMessage

If you are Using localhost then pass url like this- http://localhost:8080/api/notes defining scheme is important (http/https) do not use space also. For Your Question answer should be- http://localhost:8086/addMessage

Adding http:// or https:// scheme to URL has worked for me

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

or

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

Thanks @stacker and @Shubham

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM