Skip to content
Open
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
32 changes: 12 additions & 20 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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")
}

6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<packaging>pom</packaging>
<packaging>jar</packaging>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<version>3.2.5</version>
</parent>

<dependencies>
Expand All @@ -35,7 +35,7 @@
</dependencies>

<properties>
<java.version>1.8</java.version>
<java.version>17</java.version>
</properties>


Expand Down
80 changes: 2 additions & 78 deletions src/main/java/hello/Application.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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);
}
}
45 changes: 45 additions & 0 deletions src/main/java/hello/DataInitializer.java
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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()));
}
}
15 changes: 15 additions & 0 deletions src/main/java/hello/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
7 changes: 0 additions & 7 deletions src/main/java/hello/controller/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

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;
import org.springframework.web.bind.annotation.RequestMapping;

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 {
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/hello/model/Quote.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/java/hello/model/Topic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hello.model;

import java.util.Objects;

public class Topic {
private String id;
Expand Down Expand Up @@ -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);
}
}
33 changes: 0 additions & 33 deletions src/main/java/hello/model/Value.java

This file was deleted.

11 changes: 7 additions & 4 deletions src/main/java/hello/service/TopicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +20,7 @@
public class TopicService {


private List<Topic> topics = new ArrayList<>(Arrays.asList(
private List<Topic> 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")
Expand All @@ -36,7 +37,8 @@ public List<Topic> 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) {
Expand Down Expand Up @@ -96,8 +98,9 @@ private static List<Topic> printTopicsWithPredicate(List<Topic> topicList, Custo
* @return
*/
public List<Topic> sortTopicsWithID() {
topics.sort(Comparator.comparing(Topic::getId));
return topics;
List<Topic> sorted = new ArrayList<>(topics);
sorted.sort(Comparator.comparing(Topic::getId));
return sorted;
}


Expand Down