1717import org .springframework .context .annotation .Bean ;
1818import org .springframework .jdbc .core .JdbcTemplate ;
1919import org .springframework .web .client .RestTemplate ;
20+ import org .springframework .web .client .RestClientException ;
2021
2122@ SpringBootApplication
2223public class Application implements CommandLineRunner {
2324
2425 private static final Logger log = LoggerFactory .getLogger (Application .class );
26+ private static final String QUOTERS_URL = "http://gturnquist-quoters.cfapps.io/api/random" ;
2527
2628 public static void main (String [] args ) {
2729
@@ -36,8 +38,7 @@ public static void main(String[] args) {
3638 }
3739
3840 RestTemplate restTemplate = new RestTemplate ();
39- Quote quote = restTemplate .getForObject ("http://gturnquist-quoters.cfapps.io/api/random" , Quote .class );
40- log .info (quote .toString ());
41+ logRandomQuote (restTemplate );
4142 }
4243
4344
@@ -49,12 +50,19 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) {
4950 @ Bean
5051 public CommandLineRunner run (RestTemplate restTemplate ) throws Exception {
5152 return args -> {
52- Quote quote = restTemplate .getForObject (
53- "http://gturnquist-quoters.cfapps.io/api/random" , Quote .class );
54- log .info (quote .toString ());
53+ logRandomQuote (restTemplate );
5554 };
5655 }
5756
57+ private static void logRandomQuote (RestTemplate restTemplate ) {
58+ try {
59+ Quote quote = restTemplate .getForObject (QUOTERS_URL , Quote .class );
60+ log .info (quote .toString ());
61+ } catch (RestClientException ex ) {
62+ log .warn ("Unable to retrieve random quote from {}" , QUOTERS_URL , ex );
63+ }
64+ }
65+
5866
5967 @ Autowired
6068 JdbcTemplate jdbcTemplate ;
0 commit comments