Skip to content

Commit 9dd011f

Browse files
committed
added simple post and get endpoints for users AND made a docker-compose file, so spring boot can connect to a postgres database and could also be accessed with the pgadmin tool
1 parent 4b7b9ce commit 9dd011f

File tree

9 files changed

+182
-1
lines changed

9 files changed

+182
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
# Content Video 1
22
Dockerize a Java Spring Boot Starter Template
33

4-
<<link to youtube video>>
4+
https://www.youtube.com/watch?v=EwwkEDz3VeE
55
(published on 19th of November 2020)
66

7+
# Content Video 2
8+
- Add a docker-compose file and run also a postgres db and postgres adminer
9+
- Connect with Spring Boot to the db
10+
- Post Data && Get Data over http (to Spring Boot) and then save it in the db
11+
712
# You need
813
- Java
914
- Maven
1015
- Docker
1116
- Spring Initiliazr (e.g. in Spring Initializr Extension in VsCode or https://start.spring.io/)
1217

18+
# Start Commands for docker-compose file
19+
Builds, (re)creates, starts, and attaches to containers for a service.
20+
`docker-compose up`
21+
1322
# Start Commands for Docker
23+
Please follow the 1st Video, because due to docker-compose there are some changes
24+
1425
Build your image:
1526
`docker build <your path> -t <<user>/project-name>`
1627

docker-compose.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: "3.8"
2+
services:
3+
### Postgres Database ###
4+
postgres:
5+
image: postgres:10.4
6+
ports:
7+
- "35000:5432"
8+
environment:
9+
POSTGRES_USER: user
10+
POSTGRES_PASSWORD: password
11+
POSTGRES_DB: db
12+
### Postgres Adminer ###
13+
postgres_admin:
14+
image: dpage/pgadmin4:4.28
15+
depends_on:
16+
- postgres
17+
environment:
18+
PGADMIN_DEFAULT_EMAIL: admin@admin.de
19+
PGADMIN_DEFAULT_PASSWORD: password
20+
ports:
21+
- "5050:80"
22+
### API Spring Boot ###
23+
api:
24+
build:
25+
dockerfile: Dockerfile
26+
context: ./spring-boot-dockerized/
27+
depends_on:
28+
- postgres
29+
environment:
30+
DATABASE_URL: postgresql://postgres:5432/db
31+
DATABASE_USER: user
32+
DATABASE_PASSWORD: password
33+
ports:
34+
- "8080:3100"

spring-boot-dockerized/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@
3535
<artifactId>spring-boot-starter-web</artifactId>
3636
</dependency>
3737

38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-data-jpa</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.postgresql</groupId>
45+
<artifactId>postgresql</artifactId>
46+
<scope>runtime</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>com.h2database</groupId>
51+
<artifactId>h2</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.projectlombok</groupId>
57+
<artifactId>lombok</artifactId>
58+
<version>1.18.8</version>
59+
<scope>provided</scope>
60+
</dependency>
61+
3862
</dependencies>
3963

4064
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thomas_oliver.springbootdockerized.controller;
2+
3+
import java.util.List;
4+
5+
import com.thomas_oliver.springbootdockerized.model.User;
6+
import com.thomas_oliver.springbootdockerized.service.UserService;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
@RestController
16+
@RequestMapping("/users")
17+
public class UserController {
18+
19+
@Autowired
20+
UserService userService;
21+
22+
@GetMapping
23+
public List<User> findAll() {
24+
return userService.findAll();
25+
}
26+
27+
@PostMapping
28+
public User add(@RequestBody User user) {
29+
return userService.add(user);
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.thomas_oliver.springbootdockerized.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
9+
import lombok.Data;
10+
11+
@Entity
12+
@Table(name = "users")
13+
@Data
14+
public class User {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.AUTO)
18+
private Long id;
19+
20+
private String name;
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.thomas_oliver.springbootdockerized.repository;
2+
3+
import com.thomas_oliver.springbootdockerized.model.User;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
@Repository
9+
public interface UserRepository extends JpaRepository<User, Long>{
10+
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.thomas_oliver.springbootdockerized.service;
2+
3+
import java.util.List;
4+
5+
import com.thomas_oliver.springbootdockerized.model.User;
6+
import com.thomas_oliver.springbootdockerized.repository.UserRepository;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class UserService {
13+
14+
@Autowired
15+
private UserRepository userRepository;
16+
17+
18+
public List<User> findAll () {
19+
return userRepository.findAll();
20+
}
21+
22+
public User add(User user) {
23+
return userRepository.saveAndFlush(user);
24+
}
25+
26+
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
server.port = 3100
2+
3+
spring.datasource.driver-class-name=org.postgresql.Driver
4+
spring.datasource.initialization-mode=always
5+
spring.datasource.platform=postgres
6+
spring.datasource.url=jdbc:${DATABASE_URL}
7+
spring.datasource.username=${DATABASE_USER}
8+
spring.datasource.password=${DATABASE_PASSWORD}
9+
10+
spring.jpa.hibernate.ddl-auto=update
11+
spring.jpa.show-sql=true
12+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
13+
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.datasource.url=jdbc:h2:mem:testdb
2+
spring.datasource.username=sa
3+
spring.datasource.password=
4+
5+
spring.h2.console.enabled=false
6+
7+
spring.jpa.show-sql=true
8+
spring.jpa.hibernate.ddl-auto=update
9+
spring.jpa.properties.hibernate.format_sql=true

0 commit comments

Comments
 (0)