diff --git a/build.gradle b/build.gradle index 09f1083..5c8c142 100644 --- a/build.gradle +++ b/build.gradle @@ -1,32 +1,24 @@ -buildscript { - repositories { - mavenCentral() - } - dependencies { - classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE") - } +plugins { + id 'java' + id 'eclipse' + id 'idea' + id 'org.springframework.boot' version '3.2.5' + id 'io.spring.dependency-management' version '1.1.5' } -apply plugin: 'java' -apply plugin: 'eclipse' -apply plugin: 'idea' -apply plugin: 'org.springframework.boot' -apply plugin: 'io.spring.dependency-management' - bootJar { - baseName = 'gs-spring-boot' - version = '0.1.0' + archiveBaseName = 'gs-spring-boot' + archiveVersion = '0.1.0' } repositories { mavenCentral() } -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +sourceCompatibility = 17 +targetCompatibility = 17 dependencies { - compile("org.springframework.boot:spring-boot-starter-web") - testCompile("junit:junit") + implementation("org.springframework.boot:spring-boot-starter-web") + testImplementation("junit:junit") } - diff --git a/pom.xml b/pom.xml index 63f5cbd..f0b6dbf 100644 --- a/pom.xml +++ b/pom.xml @@ -5,13 +5,13 @@ org.springframework gs-spring-boot - pom + jar 0.1.0 org.springframework.boot spring-boot-starter-parent - 2.0.2.RELEASE + 3.2.5 @@ -35,7 +35,7 @@ - 1.8 + 17 diff --git a/src/main/java/hello/Application.java b/src/main/java/hello/Application.java index 7cf8faf..5abd411 100644 --- a/src/main/java/hello/Application.java +++ b/src/main/java/hello/Application.java @@ -1,88 +1,12 @@ package hello; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import hello.model.Customer; -import hello.model.Quote; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -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.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.web.client.RestTemplate; @SpringBootApplication -public class Application implements CommandLineRunner { - - private static final Logger log = LoggerFactory.getLogger(Application.class); +public class Application { public static void main(String[] args) { - - ApplicationContext ctx = SpringApplication.run(Application.class, args); - - System.out.println("Let's inspect the beans provided by Spring Boot:"); - - String[] beanNames = ctx.getBeanDefinitionNames(); - Arrays.sort(beanNames); - for (String beanName : beanNames) { - 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()); - } - - - @Bean - public RestTemplate restTemplate(RestTemplateBuilder builder) { - return builder.build(); - } - - @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()); - }; - } - - - @Autowired - JdbcTemplate jdbcTemplate; - - @Override - public void run(String... args) throws Exception { - log.info("Creating tables"); - - jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); - 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()); - - // 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]))); - - // Uses JdbcTemplate's batchUpdate operation to bulk load data - jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); - - 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")) - ).forEach(customer -> log.info(customer.toString())); - + SpringApplication.run(Application.class, args); } } diff --git a/src/main/java/hello/DataInitializer.java b/src/main/java/hello/DataInitializer.java new file mode 100644 index 0000000..f310b41 --- /dev/null +++ b/src/main/java/hello/DataInitializer.java @@ -0,0 +1,45 @@ +package hello; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import hello.model.Customer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +public class DataInitializer implements CommandLineRunner { + + private static final Logger log = LoggerFactory.getLogger(DataInitializer.class); + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Override + public void run(String... args) throws Exception { + log.info("Creating tables"); + + jdbcTemplate.execute("DROP TABLE IF EXISTS customers"); + jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); + + List splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long") + .stream() + .map(name -> name.split(" ")) + .collect(Collectors.toList()); + + splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1]))); + + jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); + + 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")) + ).forEach(customer -> log.info(customer.toString())); + } +} diff --git a/src/main/java/hello/config/AppConfig.java b/src/main/java/hello/config/AppConfig.java new file mode 100644 index 0000000..e7dd37c --- /dev/null +++ b/src/main/java/hello/config/AppConfig.java @@ -0,0 +1,15 @@ +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 AppConfig { + + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.build(); + } +} diff --git a/src/main/java/hello/controller/HelloController.java b/src/main/java/hello/controller/HelloController.java index f3602fa..853f222 100644 --- a/src/main/java/hello/controller/HelloController.java +++ b/src/main/java/hello/controller/HelloController.java @@ -2,7 +2,6 @@ import hello.declaration.TimeClient; import hello.model.SimpleTimeClient; -import hello.model.Topic; import hello.service.TopicService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; @@ -10,14 +9,8 @@ import java.time.LocalDate; import java.time.LocalDateTime; -import java.time.LocalTime; import java.time.ZoneId; -import java.time.chrono.ChronoPeriod; import java.time.temporal.ChronoUnit; -import java.util.List; -import java.util.regex.Pattern; -import java.util.stream.Collector; -import java.util.stream.Collectors; @RestController public class HelloController { diff --git a/src/main/java/hello/model/Quote.java b/src/main/java/hello/model/Quote.java deleted file mode 100644 index 2178a6d..0000000 --- a/src/main/java/hello/model/Quote.java +++ /dev/null @@ -1,34 +0,0 @@ -package hello.model; - -public class Quote { - private String type; - private Value value; - - @Override - public String toString() { - return "Quote{" + - "type='" + type + '\'' + - ", value=" + value + - '}'; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Value getValue() { - return value; - } - - public void setValue(Value value) { - this.value = value; - } - - public Quote() { - - } -} diff --git a/src/main/java/hello/model/Topic.java b/src/main/java/hello/model/Topic.java index 483a4a7..6f04ef3 100644 --- a/src/main/java/hello/model/Topic.java +++ b/src/main/java/hello/model/Topic.java @@ -1,5 +1,6 @@ package hello.model; +import java.util.Objects; public class Topic { private String id; @@ -40,4 +41,27 @@ public Topic(String id, String subjectName, String subjectDescription) { this.subjectDescription = subjectDescription; } + @Override + public String toString() { + return "Topic{" + + "id='" + id + '\'' + + ", subjectName='" + subjectName + '\'' + + ", subjectDescription='" + subjectDescription + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Topic topic = (Topic) o; + return Objects.equals(id, topic.id) && + Objects.equals(subjectName, topic.subjectName) && + Objects.equals(subjectDescription, topic.subjectDescription); + } + + @Override + public int hashCode() { + return Objects.hash(id, subjectName, subjectDescription); + } } diff --git a/src/main/java/hello/model/Value.java b/src/main/java/hello/model/Value.java deleted file mode 100644 index 272be70..0000000 --- a/src/main/java/hello/model/Value.java +++ /dev/null @@ -1,33 +0,0 @@ -package hello.model; - -public class Value { - private Long id; - private String quote; - - public Value() { - } - - @Override - public String toString() { - return "Value{" + - "id=" + id + - ", quote='" + quote + '\'' + - '}'; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getQuote() { - return quote; - } - - public void setQuote(String quote) { - this.quote = quote; - } -} diff --git a/src/main/java/hello/service/TopicService.java b/src/main/java/hello/service/TopicService.java index feffb92..9bc62f4 100644 --- a/src/main/java/hello/service/TopicService.java +++ b/src/main/java/hello/service/TopicService.java @@ -10,6 +10,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -19,7 +20,7 @@ public class TopicService { - private List topics = new ArrayList<>(Arrays.asList( + private List topics = new CopyOnWriteArrayList<>(Arrays.asList( new Topic("spring", "Spring Framework", "Spring Framework Description"), new Topic("java", "Core Java", "Java Description"), new Topic("javascript", "javascript Framework", "javascript Framework Description") @@ -36,7 +37,8 @@ public List getAllTopics() { * @return */ public Topic getTopicWithId(String id) { - return topics.stream().filter(topic -> topic.getId().equals(id)).findFirst().get(); + return topics.stream().filter(topic -> topic.getId().equals(id)).findFirst() + .orElseThrow(() -> new NoSuchElementException("Topic not found with id: " + id)); } public void addTopic(Topic topic) { @@ -96,8 +98,9 @@ private static List printTopicsWithPredicate(List topicList, Custo * @return */ public List sortTopicsWithID() { - topics.sort(Comparator.comparing(Topic::getId)); - return topics; + List sorted = new ArrayList<>(topics); + sorted.sort(Comparator.comparing(Topic::getId)); + return sorted; }