Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion application.properties

This file was deleted.

18 changes: 10 additions & 8 deletions src/main/java/hello/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long")
.stream()
.map(name -> name.split(" "))
.collect(Collectors.toList());
List<Object[]> splitUpNames = Stream.of("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long")
.<Object[]>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])));
Expand All @@ -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());
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/hello/config/RestClientConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
15 changes: 0 additions & 15 deletions src/main/java/hello/config/RestTemplateConfig.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Loading