Skip to content

Instantly share code, notes, and snippets.

@j-tim
Created July 13, 2019 09:13
Show Gist options
  • Save j-tim/6404d05adb7c54aa49aa5cceeda971d9 to your computer and use it in GitHub Desktop.
Save j-tim/6404d05adb7c54aa49aa5cceeda971d9 to your computer and use it in GitHub Desktop.
Autowire the RestTemplate bean in the controller
package io.stockgeeks.hello.world.api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Slf4j
public class HelloWorldController {
private final RestTemplate restTemplate;
public HelloWorldController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/api/hello")
public Greeting sayHello() {
log.info("Received call on /api/hello. And will call /api/random-name on the random-name-service!");
RandomName randomName = restTemplate.getForObject("http://localhost:8081/api/random-name", RandomName.class);
log.info("Received response from random-name-service: {}", randomName);
return new Greeting("Hello World: " + randomName.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment