-
Notifications
You must be signed in to change notification settings - Fork 3
Creating a Spring Boot Project in IntelliJ IDEA
- Open IntelliJ IDEA.
- On the Welcome screen or from the top menu bar, select
File -> New -> Project....
- In the New Project wizard, select
Spring Initializr. - Click
Next.
-
Service URL: Keep the default value
https://start.spring.io/. -
Project Metadata:
-
Group:
com.sfa -
Artifact:
saayam-request-service -
Name:
SaayamRequestService -
Description:
Saayam Request Service Project -
Package name:
com.sfa.saayamrequestservice -
Packaging:
Jar -
Java:
17
-
Group:
-
Project: Select
Mavenas the project build tool. -
Make sure to check the option
Create Git repository. -
Click
Next.
-
On the dependencies selection page, add the following dependencies:
-
Spring Boot DevTools: Development tools for hot deployment. -
Spring Web: For building web applications, including RESTful web services. -
Spring Data JPA: Provides data access abstraction to work with JPA. -
PostgreSQL Driver: PostgreSQL database driver for connecting to PostgreSQL. -
Spring Boot Actuator: For monitoring and managing application production-ready features (optional). -
Lombok: A library to simplify Java code with annotations, reducing boilerplate code (optional).
-
-
To add dependencies, enter the dependency name in the
Search for dependenciesbox and click on the displayed result to add.
- Verify that all options and dependencies are correct.
- Click
Create.
-
In the project root directory, create a file named
Dockerfilewith the following content:FROM openjdk:17-jdk-alpine WORKDIR /app COPY target/*.jar app.jar ENTRYPOINT ["java", "-jar", "app.jar"]
-
In the project root directory, create a file named
docker-compose.ymlwith the following content:version: '3.8' services: postgres: image: postgres:latest environment: POSTGRES_DB: saayam POSTGRES_USER: yourUsername POSTGRES_PASSWORD: yourPassword ports: - "5438:5432" # Ensure the port is not occupied volumes: - postgres-data:/var/lib/postgresql/data app: build: . depends_on: - postgres # Ensure postgres starts before the app ports: - "8080:8080" environment: SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/saayam SPRING_DATASOURCE_USERNAME: yourUsername SPRING_DATASOURCE_PASSWORD: yourPassword SPRING_JPA_HIBERNATE_DDL_AUTO: update volumes: postgres-data:
Ensure the project structure is as follows after adding Docker support:
saayam-request-service
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── sfa
│ │ │ └── saayamrequestservice
│ │ │ └── SaayamRequestServiceApplication.java
│ │ ├── resources
│ │ └── application.properties
│ ├── test
│ ├── java
│ └── com
│ └── sfa
│ └── saayamrequestservice
│ └── SaayamRequestServiceApplicationTests.java
├── pom.xml
├── README.md
├── Dockerfile
└── docker-compose.yml
-
Open
src/main/resources/application.propertiesand add the following configuration:spring.datasource.url=jdbc:postgresql://localhost:5438/saayam spring.datasource.username=yourUsername spring.datasource.password=yourPassword spring.jpa.hibernate.ddl-auto=update spring.jpa.open-in-view=false
Note: Replace
yourUsernameandyourPasswordwith your actual PostgreSQL username and password. In this example, the port5438is used because port5432was already in use on my machine. You can choose any other port that is not being used.
-
Open the terminal in IntelliJ IDEA (you can find it at the bottom or use
View -> Tool Windows -> Terminal). -
Run the following command to package the project without running tests, as the database might not be ready yet:
mvn package -DskipTests
Explanation: Normally, you should use
mvn clean packageto build the project. However, in this case, we use the option to skip tests(mvn package -DskipTests)because our application will attempt to connect to a database that is not yet set up and running. -
Ensure the
targetdirectory in the project root contains thesaayam-request-service-0.0.1-SNAPSHOT.jarfile. You can verify this by navigating to thetargetdirectory and listing its contents:ls target
-
Run the following command to build the Docker image and start the services:
docker-compose up --build -d
-
Check the status of the Docker containers:
Ensure both PostgreSQL and the Spring Boot application are running.
- Launch the DataGrip application.
- Click the
+icon in the top left corner, selectData Source, then choosePostgreSQL.
-
Host: Enter
localhost. -
Port: Enter
5438(normally 5432). -
Database: Enter
saayam. -
User: Enter
yourUsername. -
Password: Enter
yourPassword.
Note: You can also use IntelliJ IDEA’s built-in database tools or other free tools like pgAdmin to verify the connection.
- Click the
Test Connectionbutton to ensure the connection is successful. - If the connection is successful, click
OKandApplyto save the connection configuration.
Through the above steps, you can successfully create, configure, build, and verify a Spring Boot project with a PostgreSQL database using Docker.