Skip to content

Commit f9491c7

Browse files
committed
first commit
0 parents  commit f9491c7

File tree

10 files changed

+457
-0
lines changed

10 files changed

+457
-0
lines changed

.env

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
POSTGRESDB_USER=postgres
2+
POSTGRESDB_ROOT_PASSWORD=123456
3+
POSTGRESDB_DATABASE=bezkoder_db
4+
POSTGRESDB_LOCAL_PORT=5433
5+
POSTGRESDB_DOCKER_PORT=5432
6+
7+
SPRING_LOCAL_PORT=6868
8+
SPRING_DOCKER_PORT=8080

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Docker Compose Spring Boot and Postgres example
2+
3+
## Run the System
4+
We can easily run the whole with only a single command:
5+
```bash
6+
docker compose up
7+
```
8+
9+
Docker will pull the PostgreSQL and Spring Boot images (if our machine does not have it before).
10+
11+
The services can be run on the background with command:
12+
```bash
13+
docker compose up -d
14+
```
15+
16+
## Stop the System
17+
Stopping all the running containers is also simple with a single command:
18+
```bash
19+
docker compose down
20+
```
21+
22+
If you need to stop and remove all containers, networks, and all images used by any service in <em>docker-compose.yml</em> file, use the command:
23+
```bash
24+
docker compose down --rmi all
25+
```
26+
27+
For more detail, please visit:
28+
> [Docker Compose Spring Boot and Postgres](https://www.bezkoder.com/docker-compose-spring-boot-postgres/)
29+
30+
Related Posts:
31+
> [Spring Boot + PostgreSQL: CRUD Rest API example](https://www.bezkoder.com/spring-boot-postgresql-example/)
32+
33+
> [Spring Boot R2DBC + PostgreSQL example](https://www.bezkoder.com/spring-boot-r2dbc-postgresql/)
34+
35+
> [Spring Boot Validate Request Body](https://www.bezkoder.com/spring-boot-validate-request-body/)
36+
37+
> [Spring Boot and Swagger 3 example](https://www.bezkoder.com/spring-boot-swagger-3/)
38+
39+
> [Spring Boot Redis Cache example](https://www.bezkoder.com/spring-boot-redis-cache-example/)
40+
41+
> [Spring Boot File upload example](https://www.bezkoder.com/spring-boot-file-upload/)
42+
43+
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://www.bezkoder.com/spring-boot-restcontrolleradvice/)
44+
45+
> [Spring Boot Repository Unit Test with @DataJpaTest](https://www.bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
46+
47+
> [Spring Boot Rest Controller Unit Test with @WebMvcTest](https://www.bezkoder.com/spring-boot-webmvctest/)
48+
49+
Security:
50+
> [Secure Spring Boot App with Spring Security & JWT Authentication (PostgreSQL)](https://www.bezkoder.com/spring-boot-security-postgresql-jwt-authentication/)

bezkoder-app/Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# FROM maven:3.8.2-jdk-8 # for Java 8
2+
FROM maven:3.8.5-openjdk-17
3+
4+
WORKDIR /bezkoder-app
5+
COPY . .
6+
RUN mvn clean install
7+
8+
CMD mvn spring-boot:run

bezkoder-app/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Spring Boot JPA PostgreSQL example with Maven
2+
3+
For instruction, please visit:
4+
> [Spring Boot PostgreSQL with Maven example using Spring Data JPA](https://www.bezkoder.com/spring-boot-postgresql-example/)
5+
6+
Front-end that works well with this Back-end
7+
> [Angular 8](https://www.bezkoder.com/angular-crud-app/) / [Angular 10](https://www.bezkoder.com/angular-10-crud-app/) / [Angular 11](https://www.bezkoder.com/angular-11-crud-app/) / [Angular 12](https://www.bezkoder.com/angular-12-crud-app/) / [Angular 13](https://www.bezkoder.com/angular-13-crud-example/) / [Angular 14](https://www.bezkoder.com/angular-14-crud-example/) / [Angular 15](https://www.bezkoder.com/angular-15-crud-example/) / [Angular 16 Client](https://www.bezkoder.com/angular-16-crud-example/)
8+
9+
> [Vue 2](https://www.bezkoder.com/vue-js-crud-app/) / [Vue 3](https://www.bezkoder.com/vue-3-crud/) / [Vuetify Client](https://www.bezkoder.com/vuetify-data-table-example/)
10+
11+
> [React](https://www.bezkoder.com/react-crud-web-api/) / [React Redux Client](https://www.bezkoder.com/react-redux-crud-example/)
12+
13+
More practice:
14+
> [Spring Boot Validate Request Body](https://www.bezkoder.com/spring-boot-validate-request-body/)
15+
16+
> [Spring Boot File upload example with Multipart File](https://www.bezkoder.com/spring-boot-file-upload/)
17+
18+
> [Spring Boot Pagination & Filter example | Spring JPA, Pageable](https://www.bezkoder.com/spring-boot-pagination-filter-jpa-pageable/)
19+
20+
> [Spring Data JPA Sort/Order by multiple Columns | Spring Boot](https://www.bezkoder.com/spring-data-sort-multiple-columns/)
21+
22+
> [Spring Boot Repository Unit Test with @DataJpaTest](https://www.bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
23+
24+
> [Spring Boot Rest Controller Unit Test with @WebMvcTest](https://www.bezkoder.com/spring-boot-webmvctest/)
25+
26+
> Cache the result: [Spring Boot Redis Cache example](https://www.bezkoder.com/spring-boot-redis-cache-example/)
27+
28+
> Documentation: [Spring Boot with Swagger 3 example](https://www.bezkoder.com/spring-boot-swagger-3/)
29+
30+
> Reactive Rest API: [Spring Boot WebFlux example](https://www.bezkoder.com/spring-boot-webflux-rest-api/)
31+
32+
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
33+
34+
Associations:
35+
> [Spring Boot One To One example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-one/)
36+
37+
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)
38+
39+
> [Spring Boot Many To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-many-to-many/)
40+
41+
Security:
42+
> [Spring Boot, Spring Security, PostgreSQL: JWT Authentication & Authorization example](https://www.bezkoder.com/spring-boot-security-postgresql-jwt-authentication/)
43+
44+
Exception Handling:
45+
> [Spring Boot @ControllerAdvice & @ExceptionHandler example](https://www.bezkoder.com/spring-boot-controlleradvice-exceptionhandler/)
46+
47+
> [@RestControllerAdvice example in Spring Boot](https://www.bezkoder.com/spring-boot-restcontrolleradvice/)
48+
49+
Fullstack:
50+
> [Vue.js + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-vue-js-postgresql/)
51+
52+
> [Angular 10 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-10-spring-boot-postgresql/)
53+
54+
> [Angular 11 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-11-spring-boot-postgresql/)
55+
56+
> [Angular 12 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-12-spring-boot-postgresql/)
57+
58+
> [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/)
59+
60+
> [Angular 14 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-14-postgresql/)
61+
62+
> [Angular 15 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-15-postgresql/)
63+
64+
> [Angular 16 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-16-postgresql/)
65+
66+
> [React + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-react-postgresql/)
67+
68+
Run both Back-end & Front-end in one place:
69+
> [Integrate Angular with Spring Boot Rest API](https://www.bezkoder.com/integrate-angular-spring-boot/)
70+
71+
> [Integrate React.js with Spring Boot Rest API](https://www.bezkoder.com/integrate-reactjs-spring-boot/)
72+
73+
> [Integrate Vue.js with Spring Boot Rest API](https://www.bezkoder.com/integrate-vue-spring-boot/)
74+
75+
## Run Spring Boot application
76+
```
77+
mvn spring-boot:run
78+
```
79+

bezkoder-app/pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.1.0</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.bezkoder</groupId>
12+
<artifactId>spring-boot-jpa-postgresql</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-boot-jpa-postgresql</name>
15+
<description>Spring Boot, PostgreSQL example with Maven, Spring JPA</description>
16+
17+
<properties>
18+
<java.version>17</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.postgresql</groupId>
34+
<artifactId>postgresql</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
<exclusions>
42+
<exclusion>
43+
<groupId>org.junit.vintage</groupId>
44+
<artifactId>junit-vintage-engine</artifactId>
45+
</exclusion>
46+
</exclusions>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bezkoder.spring.jpa.postgresql;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootJpaPostgresqlApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootJpaPostgresqlApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.bezkoder.spring.jpa.postgresql.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.CrossOrigin;
11+
import org.springframework.web.bind.annotation.DeleteMapping;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.RequestParam;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
import com.bezkoder.spring.jpa.postgresql.model.Tutorial;
22+
import com.bezkoder.spring.jpa.postgresql.repository.TutorialRepository;
23+
24+
@CrossOrigin(origins = "http://localhost:8081")
25+
@RestController
26+
@RequestMapping("/api")
27+
public class TutorialController {
28+
29+
@Autowired
30+
TutorialRepository tutorialRepository;
31+
32+
@GetMapping("/tutorials")
33+
public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) {
34+
try {
35+
List<Tutorial> tutorials = new ArrayList<Tutorial>();
36+
37+
if (title == null)
38+
tutorialRepository.findAll().forEach(tutorials::add);
39+
else
40+
tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);
41+
42+
if (tutorials.isEmpty()) {
43+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
44+
}
45+
46+
return new ResponseEntity<>(tutorials, HttpStatus.OK);
47+
} catch (Exception e) {
48+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
49+
}
50+
}
51+
52+
@GetMapping("/tutorials/{id}")
53+
public ResponseEntity<Tutorial> getTutorialById(@PathVariable("id") long id) {
54+
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
55+
56+
if (tutorialData.isPresent()) {
57+
return new ResponseEntity<>(tutorialData.get(), HttpStatus.OK);
58+
} else {
59+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
60+
}
61+
}
62+
63+
@PostMapping("/tutorials")
64+
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {
65+
try {
66+
Tutorial _tutorial = tutorialRepository
67+
.save(new Tutorial(tutorial.getTitle(), tutorial.getDescription(), false));
68+
return new ResponseEntity<>(_tutorial, HttpStatus.CREATED);
69+
} catch (Exception e) {
70+
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
71+
}
72+
}
73+
74+
@PutMapping("/tutorials/{id}")
75+
public ResponseEntity<Tutorial> updateTutorial(@PathVariable("id") long id, @RequestBody Tutorial tutorial) {
76+
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
77+
78+
if (tutorialData.isPresent()) {
79+
Tutorial _tutorial = tutorialData.get();
80+
_tutorial.setTitle(tutorial.getTitle());
81+
_tutorial.setDescription(tutorial.getDescription());
82+
_tutorial.setPublished(tutorial.isPublished());
83+
return new ResponseEntity<>(tutorialRepository.save(_tutorial), HttpStatus.OK);
84+
} else {
85+
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
86+
}
87+
}
88+
89+
@DeleteMapping("/tutorials/{id}")
90+
public ResponseEntity<HttpStatus> deleteTutorial(@PathVariable("id") long id) {
91+
try {
92+
tutorialRepository.deleteById(id);
93+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
94+
} catch (Exception e) {
95+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
96+
}
97+
}
98+
99+
@DeleteMapping("/tutorials")
100+
public ResponseEntity<HttpStatus> deleteAllTutorials() {
101+
try {
102+
tutorialRepository.deleteAll();
103+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
104+
} catch (Exception e) {
105+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
106+
}
107+
108+
}
109+
110+
@GetMapping("/tutorials/published")
111+
public ResponseEntity<List<Tutorial>> findByPublished() {
112+
try {
113+
List<Tutorial> tutorials = tutorialRepository.findByPublished(true);
114+
115+
if (tutorials.isEmpty()) {
116+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
117+
}
118+
return new ResponseEntity<>(tutorials, HttpStatus.OK);
119+
} catch (Exception e) {
120+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
121+
}
122+
}
123+
124+
}

0 commit comments

Comments
 (0)