Development Guides - saayam-for-all/docs GitHub Wiki

This page will contain various development guides.

Creating a Spring Boot Project in IntelliJ IDEA

1. Start IntelliJ IDEA

  • Open IntelliJ IDEA.

2. Create a New Project

  • On the Welcome screen or from the top menu bar, select File -> New -> Project....

3. Select Project Type

  • In the New Project wizard, select Spring Initializr.
  • Click Next.

4. Project Settings

  • 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
  • Project: Select Maven as the project build tool.

  • Make sure to check the option Create Git repository.

  • Click Next.

5. Select Dependencies

  • 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 dependencies box and click on the displayed result to add.

6. Complete Project Creation

  • Verify that all options and dependencies are correct.
  • Click Create.

7. Add Docker Support

7.1 Create Dockerfile

  • In the project root directory, create a file named Dockerfile with the following content:

    FROM openjdk:17-jdk-alpine
    WORKDIR /app
    COPY target/*.jar app.jar
    ENTRYPOINT ["java", "-jar", "app.jar"]
    

7.2 Create docker-compose.yml

  • In the project root directory, create a file named docker-compose.yml with 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:
    

8. Project Structure

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

9. Configure Database Connection

  • Open src/main/resources/application.properties and 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 yourUsername and yourPassword with your actual PostgreSQL username and password. In this example, the port 5438 is used because port 5432 was already in use on my machine. You can choose any other port that is not being used.

10. Build and Run the Application

10.1 Build the Project and Generate JAR

  • 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 package to 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 target directory in the project root contains the saayam-request-service-0.0.1-SNAPSHOT.jar file. You can verify this by navigating to the target directory and listing its contents:

    ls target
    

10.2 Start Docker Compose

  • Run the following command to build the Docker image and start the services:

    docker-compose up --build -d
    

10.3 Verify Docker Container Status

  • Check the status of the Docker containers:

    Ensure both PostgreSQL and the Spring Boot application are running.

11. Connect to PostgreSQL Database Using DataGrip or Other Tools

11.1 Open DataGrip

  • Launch the DataGrip application.

11.2 Add New Data Source

  • Click the + icon in the top left corner, select Data Source, then choose PostgreSQL.

11.3 Configure Connection

  • 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.

11.4 Test Connection

  • Click the Test Connection button to ensure the connection is successful.
  • If the connection is successful, click OK and Apply to 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.