diff --git a/application.properties b/application.properties deleted file mode 100644 index d3baf26..0000000 --- a/application.properties +++ /dev/null @@ -1 +0,0 @@ -#server.port = 8081 \ No newline at end of file diff --git a/src/main/java/hello/Application.java b/src/main/java/hello/Application.java index 3dc25cb..560a9bf 100644 --- a/src/main/java/hello/Application.java +++ b/src/main/java/hello/Application.java @@ -2,7 +2,7 @@ import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; +import java.util.stream.Stream; import hello.model.Customer; import hello.model.Quote; @@ -14,8 +14,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClientException; -import org.springframework.web.client.RestTemplate; @SpringBootApplication public class Application implements CommandLineRunner { @@ -42,7 +42,7 @@ public static void main(String[] args) { JdbcTemplate jdbcTemplate; @Autowired - RestTemplate restTemplate; + RestClient restClient; @Override public void run(String... args) throws Exception { @@ -54,10 +54,9 @@ public void run(String... args) throws Exception { jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); // Split up the array of whole names into an array of first/last names - List splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long") - .stream() - .map(name -> name.split(" ")) - .collect(Collectors.toList()); + List splitUpNames = Stream.of("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long") + .map(name -> name.split(" ")) + .toList(); // Use a Java 8 stream to print out each tuple of the list splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); @@ -80,7 +79,10 @@ public void run(String... args) throws Exception { */ private void logRandomQuote() { try { - Quote quote = restTemplate.getForObject(QUOTE_URL, Quote.class); + Quote quote = restClient.get() + .uri(QUOTE_URL) + .retrieve() + .body(Quote.class); log.info(String.valueOf(quote)); } catch (RestClientException e) { log.warn("Could not fetch a quote from {}: {}", QUOTE_URL, e.getMessage()); diff --git a/src/main/java/hello/config/RestClientConfig.java b/src/main/java/hello/config/RestClientConfig.java new file mode 100644 index 0000000..9cef05a --- /dev/null +++ b/src/main/java/hello/config/RestClientConfig.java @@ -0,0 +1,14 @@ +package hello.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestClient; + +@Configuration +public class RestClientConfig { + + @Bean + public RestClient restClient(RestClient.Builder builder) { + return builder.build(); + } +} diff --git a/src/main/java/hello/config/RestTemplateConfig.java b/src/main/java/hello/config/RestTemplateConfig.java deleted file mode 100644 index 0ef3329..0000000 --- a/src/main/java/hello/config/RestTemplateConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package hello.config; - -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.client.RestTemplate; - -@Configuration -public class RestTemplateConfig { - - @Bean - public RestTemplate restTemplate(RestTemplateBuilder builder) { - return builder.build(); - } -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..c4a0209 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +# Serve each request on a virtual thread (Java 21 + Spring Boot 3.2+). +spring.threads.virtual.enabled=true + +#server.port = 8081