0

I have an endpoint that I need to call from a method inside of a class.

public void remove(String str) {
    //Call Controller with str
}

The controller has /local/{str}. How do I call this controller from that method?

letsCode
  • 2,774
  • 1
  • 13
  • 37
  • Maybe this helps you: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html and https://www.baeldung.com/rest-template – pL4Gu33 Feb 06 '20 at 16:50
  • Check this out : https://stackoverflow.com/questions/42365266/call-another-rest-api-from-my-server-in-spring-boot – Habil Feb 06 '20 at 16:55
  • Does this answer your question? [Call another rest api from my server in Spring-Boot](https://stackoverflow.com/questions/42365266/call-another-rest-api-from-my-server-in-spring-boot) – Habil Feb 06 '20 at 16:56

1 Answers1

3

So I suppose that your controller is similar to this

@GetMapping("/local/{str}")
public String method(@PathVariable String str) {...}

Why not to call this method directly?

public void remove(String str) {
    method("my parameter");
}

Or you can also call this endpoint by using RestTemplate

public void remove(String str) {
    final String uri = "http://hostName/local/{str}";
    Map<String, String> params = new HashMap<String, String>();
    params.put("str", "my_String");

    RestTemplate restTemplate = new RestTemplate();
    String response = restTemplate.getForObject(uri, String.class, params);
}
Planck Constant
  • 1,406
  • 1
  • 17
  • 19