From 7d4d65c7803b97322ade51e1a6299a80aedf3052 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Thu, 12 Mar 2020 11:02:26 +0100 Subject: [PATCH 1/3] Upgrade to Spring Boot 2.2.5 Signed-off-by: Guillaume Lours --- samples/spring-postgres/backend/Dockerfile | 4 +-- samples/spring-postgres/backend/pom.xml | 34 +++++++++---------- .../project/controllers/HomeController.java | 1 - .../templates/{home.ftl => home.ftlh} | 0 4 files changed, 19 insertions(+), 20 deletions(-) rename samples/spring-postgres/backend/src/main/resources/templates/{home.ftl => home.ftlh} (100%) diff --git a/samples/spring-postgres/backend/Dockerfile b/samples/spring-postgres/backend/Dockerfile index 10e0920..abeeae4 100755 --- a/samples/spring-postgres/backend/Dockerfile +++ b/samples/spring-postgres/backend/Dockerfile @@ -1,11 +1,11 @@ -FROM maven:3.5-jdk-9 AS build +FROM maven:3.6.3-jdk-11 AS build COPY pom.xml . RUN mvn --batch-mode dependency:resolve COPY . . RUN mvn --batch-mode package RUN cp target/*jar target/app.jar -FROM openjdk:9-jre +FROM openjdk:11-jre EXPOSE 8080 VOLUME /tmp COPY --from=build target/app.jar app.jar diff --git a/samples/spring-postgres/backend/pom.xml b/samples/spring-postgres/backend/pom.xml index aeef767..99faf25 100755 --- a/samples/spring-postgres/backend/pom.xml +++ b/samples/spring-postgres/backend/pom.xml @@ -14,24 +14,28 @@ org.springframework.boot spring-boot-starter-parent - 2.0.3.RELEASE + 2.2.5.RELEASE UTF-8 UTF-8 - 1.8 + 11 org.springframework.boot - spring-boot-starter-jersey + spring-boot-starter-web org.springframework.boot - spring-boot-starter-web + spring-boot-starter-freemarker + + + org.springframework.boot + spring-boot-starter-jersey org.springframework.session @@ -40,26 +44,22 @@ org.springframework.boot spring-boot-devtools - - - org.springframework.boot - spring-boot-starter-freemarker + runtime + true org.springframework.boot spring-boot-starter-test test - - - org.projectlombok - lombok - - - javax.xml.bind - jaxb-api - 2.3.0 + + + org.junit.vintage + junit-vintage-engine + + + diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java b/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java index 966839d..a683b50 100755 --- a/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java @@ -3,7 +3,6 @@ package com.company.project.controllers; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; @Controller public class HomeController { diff --git a/samples/spring-postgres/backend/src/main/resources/templates/home.ftl b/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh similarity index 100% rename from samples/spring-postgres/backend/src/main/resources/templates/home.ftl rename to samples/spring-postgres/backend/src/main/resources/templates/home.ftlh From ddcbf6f7bda91a02e27d88043d25615282b4eac2 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Thu, 12 Mar 2020 14:16:55 +0100 Subject: [PATCH 2/3] Add connection between spring boot server and postgresql database Signed-off-by: Guillaume Lours --- samples/spring-postgres/backend/pom.xml | 9 +++ .../project/controllers/HomeController.java | 8 +++ .../com/company/project/entity/Greeting.java | 57 +++++++++++++++++++ .../repository/GreetingRepository.java | 9 +++ .../src/main/resources/application.properties | 10 ++++ .../backend/src/main/resources/data.sql | 1 + .../backend/src/main/resources/schema.sql | 4 ++ .../src/main/resources/templates/home.ftlh | 2 +- samples/spring-postgres/docker-compose.yaml | 8 +++ 9 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java create mode 100644 samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java create mode 100644 samples/spring-postgres/backend/src/main/resources/data.sql create mode 100644 samples/spring-postgres/backend/src/main/resources/schema.sql diff --git a/samples/spring-postgres/backend/pom.xml b/samples/spring-postgres/backend/pom.xml index 99faf25..b0471a1 100755 --- a/samples/spring-postgres/backend/pom.xml +++ b/samples/spring-postgres/backend/pom.xml @@ -37,6 +37,15 @@ org.springframework.boot spring-boot-starter-jersey + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.postgresql + postgresql + runtime + org.springframework.session spring-session-core diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java b/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java index a683b50..c23d706 100755 --- a/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/controllers/HomeController.java @@ -1,5 +1,8 @@ package com.company.project.controllers; +import com.company.project.entity.Greeting; +import com.company.project.repository.GreetingRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -7,8 +10,13 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { + @Autowired + private GreetingRepository repository; + @GetMapping("/") public String showHome(String name, Model model) { + Greeting dockerGreeting = repository.findById(1).orElse(new Greeting("Not Found 😕")); + model = model.addAttribute("name", dockerGreeting.getName()); return "home"; } diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java b/samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java new file mode 100644 index 0000000..67c1079 --- /dev/null +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/entity/Greeting.java @@ -0,0 +1,57 @@ +package com.company.project.entity; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "GREETINGS") +public class Greeting { + + @Id + private int id; + private String name; + + public Greeting() { + } + + public Greeting(String name) { + this.name = name; + } + + public Greeting(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Greeting greeting = (Greeting) o; + + return name.equals(greeting.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } +} diff --git a/samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java b/samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java new file mode 100644 index 0000000..8927337 --- /dev/null +++ b/samples/spring-postgres/backend/src/main/java/com/company/project/repository/GreetingRepository.java @@ -0,0 +1,9 @@ +package com.company.project.repository; + +import com.company.project.entity.Greeting; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface GreetingRepository extends CrudRepository { +} diff --git a/samples/spring-postgres/backend/src/main/resources/application.properties b/samples/spring-postgres/backend/src/main/resources/application.properties index 8b13789..e911c04 100755 --- a/samples/spring-postgres/backend/src/main/resources/application.properties +++ b/samples/spring-postgres/backend/src/main/resources/application.properties @@ -1 +1,11 @@ +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect +spring.jpa.hibernate.ddl-auto=none +spring.jpa.hibernate.show-sql=true +spring.datasource.url=jdbc:postgresql://db:5432/${POSTGRES_DB} +spring.datasource.username=postgres +spring.datasource.password=${POSTGRES_PASSWORD:db-wrz2z} +spring.datasource.initialization-mode=always +spring.datasource.initialize=true +spring.datasource.schema=classpath:/schema.sql +spring.datasource.continue-on-error=true diff --git a/samples/spring-postgres/backend/src/main/resources/data.sql b/samples/spring-postgres/backend/src/main/resources/data.sql new file mode 100644 index 0000000..122079f --- /dev/null +++ b/samples/spring-postgres/backend/src/main/resources/data.sql @@ -0,0 +1 @@ +INSERT INTO GREETINGS(name) values ('Docker'); diff --git a/samples/spring-postgres/backend/src/main/resources/schema.sql b/samples/spring-postgres/backend/src/main/resources/schema.sql new file mode 100644 index 0000000..bd40371 --- /dev/null +++ b/samples/spring-postgres/backend/src/main/resources/schema.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS GREETINGS ( + id serial PRIMARY KEY, + name varchar(50) NOT NULL +); diff --git a/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh b/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh index daba182..42d0c29 100755 --- a/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh +++ b/samples/spring-postgres/backend/src/main/resources/templates/home.ftlh @@ -5,5 +5,5 @@ -

Hello from Docker!

+

Hello from ${name}!

diff --git a/samples/spring-postgres/docker-compose.yaml b/samples/spring-postgres/docker-compose.yaml index 307a6a7..d6cb620 100644 --- a/samples/spring-postgres/docker-compose.yaml +++ b/samples/spring-postgres/docker-compose.yaml @@ -4,6 +4,10 @@ services: build: backend ports: - 80:8080 + environment: + POSTGRES_DB: example + networks: + - spring-postges db: environment: POSTGRES_DB: example @@ -14,8 +18,12 @@ services: - db-password volumes: - db-data:/var/lib/postgresql/data + networks: + - spring-postges volumes: db-data: {} secrets: db-password: file: db/password.txt +networks: + spring-postges: {} From f4f078cd4095464dd608b1ff23e241a90886eba2 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Thu, 12 Mar 2020 14:29:50 +0100 Subject: [PATCH 3/3] Improve Dockerfile Signed-off-by: Guillaume Lours --- samples/spring-postgres/backend/Dockerfile | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/samples/spring-postgres/backend/Dockerfile b/samples/spring-postgres/backend/Dockerfile index abeeae4..949dd0a 100755 --- a/samples/spring-postgres/backend/Dockerfile +++ b/samples/spring-postgres/backend/Dockerfile @@ -1,12 +1,20 @@ -FROM maven:3.6.3-jdk-11 AS build -COPY pom.xml . -RUN mvn --batch-mode dependency:resolve -COPY . . -RUN mvn --batch-mode package -RUN cp target/*jar target/app.jar +FROM maven:3.6.3-jdk-11 AS builder +WORKDIR /workdir/server +COPY pom.xml /workdir/server/pom.xml +RUN mvn dependency:go-offline + +COPY src /workdir/server/src +RUN mvn install +RUN mkdir -p target/depency +WORKDIR /workdir/server/target/dependency +RUN jar -xf ../*.jar + +FROM openjdk:11-jre-slim -FROM openjdk:11-jre EXPOSE 8080 VOLUME /tmp -COPY --from=build target/app.jar app.jar -ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] +ARG DEPENDENCY=/workdir/server/target/dependency +COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib +COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF +COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app +ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"] \ No newline at end of file