GET/POST Requst to REST API using Spring Boot

前端 未结 4 1231
北海茫月
北海茫月 2021-01-07 08:01

I have a REST Service an external server like https://api.myrestservice.com and I have a Spring Boot Application running locally on http://localhost:8080<

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 08:11

    I hope I got your question right. You are trying get your local app to get data from app running on your server.

    You can use the below sample code in your spring boot application.

    private void getUsers() {
    
         final String uri = "https://api.myrestservice.com/users";
         RestTemplate restTemplate = new RestTemplate();
         Users result = restTemplate.getForObject(uri, Users.class);      
         System.out.println(result); 
    }
    

    Then your getUsers can be invoked by getUsers Controller in your spring boot app.

    I am adding the reference if you want to look at more examples - https://howtodoinjava.com/spring-restful/spring-restful-client-resttemplate-example/

提交回复
热议问题