Created
July 13, 2019 09:13
-
-
Save j-tim/6404d05adb7c54aa49aa5cceeda971d9 to your computer and use it in GitHub Desktop.
Autowire the RestTemplate bean in the controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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