diff --git a/pom.xml b/pom.xml index 63f5cbd..5916cb0 100644 --- a/pom.xml +++ b/pom.xml @@ -5,24 +5,27 @@ org.springframework gs-spring-boot - pom + jar 0.1.0 org.springframework.boot spring-boot-starter-parent - 2.0.2.RELEASE + 4.1.1 org.springframework.boot - spring-boot-properties-migrator - runtime + spring-boot-starter-actuator org.springframework.boot - spring-boot-starter-web + spring-boot-starter-webmvc + + + org.springframework.boot + spring-boot-starter-restclient org.springframework.boot @@ -35,7 +38,7 @@ - 1.8 + 17 diff --git a/src/main/java/hello/Application.java b/src/main/java/hello/Application.java index 7cf8faf..8e0a904 100644 --- a/src/main/java/hello/Application.java +++ b/src/main/java/hello/Application.java @@ -12,10 +12,11 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.boot.restclient.RestTemplateBuilder; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @SpringBootApplication @@ -23,6 +24,8 @@ public class Application implements CommandLineRunner { private static final Logger log = LoggerFactory.getLogger(Application.class); + private static final String QUOTE_URL = "http://gturnquist-quoters.cfapps.io/api/random"; + public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); @@ -35,9 +38,16 @@ public static void main(String[] args) { System.out.println(beanName); } - RestTemplate restTemplate = new RestTemplate(); - Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class); - log.info(quote.toString()); + logRandomQuote(new RestTemplate()); + } + + private static void logRandomQuote(RestTemplate restTemplate) { + try { + Quote quote = restTemplate.getForObject(QUOTE_URL, Quote.class); + log.info(String.valueOf(quote)); + } catch (RestClientException e) { + log.warn("Could not fetch random quote from {}: {}", QUOTE_URL, e.getMessage()); + } } @@ -48,11 +58,7 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) { @Bean public CommandLineRunner run(RestTemplate restTemplate) throws Exception { - return args -> { - Quote quote = restTemplate.getForObject( - "http://gturnquist-quoters.cfapps.io/api/random", Quote.class); - log.info(quote.toString()); - }; + return args -> logRandomQuote(restTemplate); } @@ -80,8 +86,9 @@ public void run(String... args) throws Exception { log.info("Querying for customer records where first_name = 'Josh':"); jdbcTemplate.query( - "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[]{"Josh"}, - (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")) + "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", + (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")), + "Josh" ).forEach(customer -> log.info(customer.toString())); }