From 9a3a15f03b4a806d0ce7e80ede4b2556362b7e28 Mon Sep 17 00:00:00 2001 From: uuidcode Date: Wed, 2 May 2018 17:03:40 +0900 Subject: [PATCH 1/4] handlebars --- app/pom.xml | 5 ++++ .../springboot/IndexController.java | 5 ++-- app/src/main/resources/application.properties | 12 -------- app/src/main/resources/application.yml | 30 +++++++++++++++++++ .../templates/{index.ftl => index.hbs} | 2 +- docker-compose.yaml | 2 ++ 6 files changed, 41 insertions(+), 15 deletions(-) delete mode 100644 app/src/main/resources/application.properties create mode 100644 app/src/main/resources/application.yml rename app/src/main/resources/templates/{index.ftl => index.hbs} (91%) diff --git a/app/pom.xml b/app/pom.xml index 0c98b9f..fb30359 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -27,6 +27,11 @@ mysql mysql-connector-java + + pl.allegro.tech.boot + handlebars-spring-boot-starter + 0.2.15 + diff --git a/app/src/main/java/com/hellokoding/springboot/IndexController.java b/app/src/main/java/com/hellokoding/springboot/IndexController.java index 4acea09..441d9bc 100644 --- a/app/src/main/java/com/hellokoding/springboot/IndexController.java +++ b/app/src/main/java/com/hellokoding/springboot/IndexController.java @@ -1,17 +1,18 @@ package com.hellokoding.springboot; +import java.util.logging.Logger; + import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; -import java.util.logging.Logger; - @Controller public class IndexController { private final Logger logger = Logger.getLogger(this.getClass().getName()); @GetMapping("/") public String index(Model model) { + model.addAttribute("time", System.currentTimeMillis()); return "index"; } } diff --git a/app/src/main/resources/application.properties b/app/src/main/resources/application.properties deleted file mode 100644 index ccc21b3..0000000 --- a/app/src/main/resources/application.properties +++ /dev/null @@ -1,12 +0,0 @@ -spring.freemarker.template-loader-path: classpath:/templates -spring.freemarker.suffix: .ftl - -spring.datasource.url=jdbc:mysql://mysql:3306/test?useSSL=false -spring.datasource.username=root -spring.datasource.password=hellokoding -spring.datasource.driver-class-name=com.mysql.jdbc.Driver - -spring.jpa.hibernate.ddl-auto=create -spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect -spring.jpa.generate-ddl=true -spring.jpa.show-sql=true \ No newline at end of file diff --git a/app/src/main/resources/application.yml b/app/src/main/resources/application.yml new file mode 100644 index 0000000..72fb551 --- /dev/null +++ b/app/src/main/resources/application.yml @@ -0,0 +1,30 @@ +spring: + datasource: + url: jdbc:mysql://mysql:3306/test?useSSL=false + username: root + password: hellokoding + driver-class-name: com.mysql.jdbc.Driver + + jpa: + hibernate: + ddl-auto: create + database-platform: org.hibernate.dialect.MySQLDialect + generate-ddl: true + show-sql: true + +handlebars: + enabled: true + prefix: classpath:templates/ + suffix: .hbs + cache: false + registerMessageHelper: true + failOnMissingFile: false + bindI18nToMessageSource: false + prettyPrint: false + infiniteLoops: false + expose-request-attributes: true + resolver: + javaBean: true + map: true + method: true + field: false \ No newline at end of file diff --git a/app/src/main/resources/templates/index.ftl b/app/src/main/resources/templates/index.hbs similarity index 91% rename from app/src/main/resources/templates/index.ftl rename to app/src/main/resources/templates/index.hbs index 6e29a58..f017083 100644 --- a/app/src/main/resources/templates/index.ftl +++ b/app/src/main/resources/templates/index.hbs @@ -6,6 +6,6 @@

Docker Compose with Spring Boot, MySQL, NGINX

-

+

{{time}}

\ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index aecf61b..dc95a79 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -30,6 +30,8 @@ services: - ~/.m2:/root/.m2 expose: - "8080" + ports: + - "8080:8080" command: mvn clean spring-boot:run depends_on: - nginx From 3423687365335a81a74f836c74d2394de973c69e Mon Sep 17 00:00:00 2001 From: uuidcode Date: Wed, 2 May 2018 18:00:58 +0900 Subject: [PATCH 2/4] Book --- app/pom.xml | 4 ++ .../java/com/hellokoding/springboot/Book.java | 50 +++++++++++++++++++ .../springboot/BookController.java | 33 ++++++++++++ .../springboot/BookRepository.java | 11 ++++ .../com/hellokoding/springboot/CoreUtil.java | 12 +++++ .../springboot/WebConfiguration.java | 21 ++++++++ app/src/main/resources/application.yml | 2 +- .../main/resources/templates/book/list.hbs | 22 ++++++++ docker-compose.yaml | 3 +- 9 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/hellokoding/springboot/Book.java create mode 100644 app/src/main/java/com/hellokoding/springboot/BookController.java create mode 100644 app/src/main/java/com/hellokoding/springboot/BookRepository.java create mode 100644 app/src/main/java/com/hellokoding/springboot/CoreUtil.java create mode 100644 app/src/main/java/com/hellokoding/springboot/WebConfiguration.java create mode 100644 app/src/main/resources/templates/book/list.hbs diff --git a/app/pom.xml b/app/pom.xml index fb30359..df6d10f 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -32,6 +32,10 @@ handlebars-spring-boot-starter 0.2.15 + + com.google.code.gson + gson + diff --git a/app/src/main/java/com/hellokoding/springboot/Book.java b/app/src/main/java/com/hellokoding/springboot/Book.java new file mode 100644 index 0000000..2ddd03c --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/Book.java @@ -0,0 +1,50 @@ +package com.hellokoding.springboot; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long bookId; + private String title; + private Long page; + private String author; + + public String getAuthor() { + return this.author; + } + + public Book setAuthor(String author) { + this.author = author; + return this; + } + public Long getPage() { + return this.page; + } + + public Book setPage(Long page) { + this.page = page; + return this; + } + public String getTitle() { + return this.title; + } + + public Book setTitle(String title) { + this.title = title; + return this; + } + + public Long getBookId() { + return this.bookId; + } + + public Book setBookId(Long bookId) { + this.bookId = bookId; + return this; + } +} diff --git a/app/src/main/java/com/hellokoding/springboot/BookController.java b/app/src/main/java/com/hellokoding/springboot/BookController.java new file mode 100644 index 0000000..784b746 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/BookController.java @@ -0,0 +1,33 @@ +package com.hellokoding.springboot; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +@Controller +public class BookController { + protected static Logger logger = LoggerFactory.getLogger(BookController.class); + + @Autowired + private BookRepository bookRepository; + + @PostMapping + public String post(Book book) { + if (logger.isInfoEnabled()) { + logger.info(">>> post book: {}", CoreUtil.toJson(book)); + } + + this.bookRepository.save(book); + return "redirect:/book"; + } + + @GetMapping + public String list(Model model) { + model.addAttribute("list", this.bookRepository.findAllByOrderByBookIdDesc()); + return "/book/list"; + } +} diff --git a/app/src/main/java/com/hellokoding/springboot/BookRepository.java b/app/src/main/java/com/hellokoding/springboot/BookRepository.java new file mode 100644 index 0000000..4be7788 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/BookRepository.java @@ -0,0 +1,11 @@ +package com.hellokoding.springboot; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface BookRepository extends JpaRepository { + List findAllByOrderByBookIdDesc(); +} diff --git a/app/src/main/java/com/hellokoding/springboot/CoreUtil.java b/app/src/main/java/com/hellokoding/springboot/CoreUtil.java new file mode 100644 index 0000000..4866d41 --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/CoreUtil.java @@ -0,0 +1,12 @@ +package com.hellokoding.springboot; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class CoreUtil { + private static Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + public static String toJson(Object object) { + return gson.toJson(object); + } +} diff --git a/app/src/main/java/com/hellokoding/springboot/WebConfiguration.java b/app/src/main/java/com/hellokoding/springboot/WebConfiguration.java new file mode 100644 index 0000000..4731c4f --- /dev/null +++ b/app/src/main/java/com/hellokoding/springboot/WebConfiguration.java @@ -0,0 +1,21 @@ +package com.hellokoding.springboot; + +import javax.servlet.Filter; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.web.filter.CharacterEncodingFilter; + +@Configuration +public class WebConfiguration { + @Order(Ordered.HIGHEST_PRECEDENCE) + @Bean + public Filter characterEncodingFilter() { + CharacterEncodingFilter filter = new CharacterEncodingFilter(); + filter.setEncoding("UTF-8"); + filter.setForceEncoding(true); + return filter; + } +} diff --git a/app/src/main/resources/application.yml b/app/src/main/resources/application.yml index 72fb551..fe6dd72 100644 --- a/app/src/main/resources/application.yml +++ b/app/src/main/resources/application.yml @@ -1,6 +1,6 @@ spring: datasource: - url: jdbc:mysql://mysql:3306/test?useSSL=false + url: jdbc:mysql://mysql:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8 username: root password: hellokoding driver-class-name: com.mysql.jdbc.Driver diff --git a/app/src/main/resources/templates/book/list.hbs b/app/src/main/resources/templates/book/list.hbs new file mode 100644 index 0000000..c4aff72 --- /dev/null +++ b/app/src/main/resources/templates/book/list.hbs @@ -0,0 +1,22 @@ +
+title: +page: +author: + +
+ + + + + + + + {{#list}} + + + + + + + {{/list}} +
bookIdtitlepageauthor
{{bookId}}{{title}}{{page}}{{author}}
\ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index dc95a79..9665123 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -13,6 +13,7 @@ services: mysql: container_name: some-mysql image: mysql/mysql-server:5.7 + command: --character-set-server=utf8 --collation-server=utf8_unicode_ci environment: MYSQL_DATABASE: test MYSQL_ROOT_PASSWORD: hellokoding @@ -32,7 +33,7 @@ services: - "8080" ports: - "8080:8080" - command: mvn clean spring-boot:run + command: mvn clean spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8" depends_on: - nginx - mysql From b6b51b629f61f50d3bef6e1c817f8ab0df34b655 Mon Sep 17 00:00:00 2001 From: uuidcode Date: Wed, 2 May 2018 18:15:20 +0900 Subject: [PATCH 3/4] Book --- docker-compose.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 9665123..fec954f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -30,9 +30,9 @@ services: - ./app:/app - ~/.m2:/root/.m2 expose: - - "8080" + - "8080" ports: - - "8080:8080" + - "8080:8080" command: mvn clean spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8" depends_on: - nginx From 1bfaa1df384873c5c7b951748b3ba44c0d37f0b2 Mon Sep 17 00:00:00 2001 From: uuidcode Date: Wed, 2 May 2018 18:50:08 +0900 Subject: [PATCH 4/4] remove freemarker --- app/pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/pom.xml b/app/pom.xml index df6d10f..5b39ae8 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -15,10 +15,6 @@ - - org.springframework.boot - spring-boot-starter-freemarker - org.springframework.boot spring-boot-starter-data-jpa