Building a Simple CRUD Application Using Spring Boot, Angular, and Jasper Report [Part 1 - Spring Boot Section]
In today's fast-paced digital world, developing efficient and robust web applications is crucial for businesses to stay competitive. This tutorial will guide you through building a simple CRUD (Create, Read, Update, Delete) application using Spring Boot for the backend, Angular for the frontend, and Jasper Reports for generating dynamic reports. I've divided this topic into three parts: Spring Boot, Jasper Reports, and Angular. By the end of these articles, you'll have a solid understanding of how to integrate these powerful technologies to create a seamless and functional web application. Whether you're a beginner looking to expand your skills or an experienced developer seeking to explore new tools, this step-by-step guide will provide you with the knowledge and confidence to build your own CRUD application. Let's get started!
Preparation
This guide will provide a step-by-step walkthrough of the process. However, before we delve into that, it's crucial to ensure that your development environment is equipped with the required prerequisites. Here is a list of the prerequisites for this tutorial:
Initialize Spring Boot Project
Initialize the project through the IDE or using the Spring Initializr . Next open the project and add dependencies by replacing the `<dependencies></dependencies>` section in the pom.xml file with the following code.
<dependencies>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.21.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.10</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.12.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.12.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.12.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
After updating your pom.xml, run the following Maven command to update your project dependencies.
mvn clean install
Next, open the project and configure the database and other settings in the ./src/main/resources/application.properties file.
Initialize Dockerfile
Create file ./Dockerfile and check line33 to ensure it matches your project identity as specified in ./pom.xml file
Here's a breakdown of what each section does:
Initialize SQL Init File
Create simple file. This file will be executed within docker.
Initialize Docker Compose File
Create ./docker-compose.yml file. For development purposes, you can comment out the app section and uncomment it when you are ready to dockerize your app.
You can run this using the following command, and your development environment will be ready to use.
docker compose up -d
Build Back End Apps
My project has the groupId "com.etwicaksono" and the artifactId "simple-online-shop," so my program files are placed in ./src/main/java/com/etwicaksono/simpleonlineshop directory. Your path may be a bit different from mine, so adjust it based on your setup.
Configuration
A configuration class in Spring Boot is a class annotated with @Configuration that is used to define beans and settings for your Spring application context. These classes are crucial for setting up and managing various aspects of your application, such as data sources, security, and custom beans.
Here are the key concepts related to configuration in Spring Boot:
Configuration classes in Spring Boot provide a flexible and powerful way to define beans and settings for your application. By using the @Configuration and @Bean annotations, you can manage your application's dependencies and configurations efficiently. Profiles allow for environment-specific configurations, ensuring that your application can adapt to different environments seamlessly.
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/config to store all configurations we use. Create the following files as our configurations.
Entities
In Java Spring Boot, an entity is a lightweight, persistent domain object that typically represents a table in a relational database. Each instance of the entity corresponds to a row in the table. Entities are used in conjunction with the Java Persistence API (JPA) to map the object-oriented domain model to a relational database.
Here are some key points about entities in Java Spring Boot:
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/entity to store all entities we use. Create the following files as our entities.
Repositories
In Spring Boot, repositories are used to encapsulate the logic required to access data sources. Repositories provide a convenient, consistent way to interact with databases, making CRUD (Create, Read, Update, Delete) operations straightforward and reducing boilerplate code. Spring Data JPA is a powerful feature of Spring Boot that simplifies the development of repositories.
Here are the key concepts related to repositories in Spring Boot:
Repositories in Spring Boot provide a powerful and easy way to handle data access. By extending the repository interfaces provided by Spring Data JPA, you can leverage a wide range of data access methods without writing boilerplate code. Custom queries and automatic transaction management further enhance the capabilities of repositories, making data access efficient and manageable.
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/repository to store all repositories we use. Create the following files as our repositories.
Utils
In Spring Boot, utility classes (often referred to as "utils") serve as a collection of static methods or reusable code that can be used across the application. These classes are designed to perform common tasks that are needed in multiple places, helping to reduce code duplication and improve maintainability.
Here are the key concepts related to utils in Spring Boot:
Utility classes in Spring Boot provide a centralized way to implement common, reusable functionality. By encapsulating frequently used methods in utility classes, you can keep your codebase clean, maintainable, and DRY (Don't Repeat Yourself). These classes should be stateless and consist of static methods, ensuring they are easy to use and thread-safe.
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/utils to store all utils we use. Create the following files as our utils.
Data Transfer Object (DTO)
In Spring Boot, a Data Transfer Object (DTO) is an object that carries data between processes. DTOs are often used to encapsulate the data for transfer between the client and server, particularly in RESTful APIs. They serve as a simple, plain Java object (POJO) designed to hold data with no business logic.
Here are the key concepts related to dto in Spring Boot:
DTOs in Spring Boot are essential for transferring data between different layers of the application, particularly between the client and server in RESTful APIs. By using DTOs, you achieve a clear separation of concerns, facilitate data validation, and simplify data serialization and deserialization. DTOs help maintain clean and maintainable code, ensuring that the internal domain models remain decoupled from external data representations.
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/dto to store all dto we use. Create the following files as our dto.
General DTO
Customer DTO
领英推荐
Item DTO
Order DTO
Specification
In Spring Boot, specifications are used to build dynamic queries for retrieving data from a database. The Specification pattern, provided by Spring Data JPA, allows you to create complex, flexible, and type-safe queries using a programmatic approach. This is particularly useful when you need to build queries based on multiple criteria that can vary at runtime.
Here are the key concepts related to specification in Spring Boot:
Specifications in Spring Boot, provided by Spring Data JPA, offer a powerful and flexible way to build dynamic and type-safe queries. By using the Specification interface and the Criteria API, you can create complex queries based on multiple criteria that can be easily combined and reused. This approach promotes clean, maintainable, and flexible code, making it easier to handle complex querying requirements in your application.
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/service/specification to store all specifications we use. Create the following files as our specifications.
Service
In Spring Boot, the service layer is responsible for encapsulating the business logic of the application. It serves as an intermediary between the controller layer, which handles incoming HTTP requests, and the repository layer, which interacts with the database. By centralizing business logic in services, you ensure a clean separation of concerns, making your code more modular, testable, and maintainable.
Here are the key concepts related to service in Spring Boot:
We'll use directory ./src/main/java/com/etwicaksono/simpleonlineshop/service to store all services we use. Create the following files as our services.
Controller
A controller in Spring Boot is a central part of the Spring MVC framework, responsible for handling web requests and mapping them to appropriate service methods. It acts as an intermediary between the client and the server-side logic, processing incoming requests, invoking business logic, and returning appropriate responses.
Key components and features of a Spring Boot controller include:
By encapsulating the routing logic, input validation, and interaction with service layers, controllers in Spring Boot provide a clean, organized way to handle web requests and responses, facilitating the development of robust and maintainable web applications.
Create directory ./src/main/java/com/etwicaksono/simpleonlineshop/service/controller to store all controllers we use. Create the following files as our controllers.
Look at line 256; there is a file for generating reports, which we will discuss further in the Jasper Reports section. As a temporary solution, you can take Customer_List.jasper file from my repository.
This class is for overriding the default error handling provided by Spring Boot.
Look at line 240; there is a file for generating reports, which we will discuss further in the Jasper Reports section. As a temporary solution, you can take Item_List.jasper file from my repository.
Look at line 218; there is a file for generating reports, which we will discuss further in the Jasper Reports section. As a temporary solution, you can take Order_List.jasper file from my repository.
We use rootHandler to show first page, it needs a html file to show up, so let's create ./src/main/resources/templates/welcome.html file.
Run the Application
Navigate to Project Directory: Open a terminal (Command Prompt on Windows, Terminal on macOS/Linux) and navigate to the root directory of your Spring Boot project.
cd /path/to/your/project
Build the Project (if necessary): If your project hasn't been built yet or if you've made changes to the code, you may need to build it using Maven or Gradle. Spring Boot projects typically use Maven by default.
# For Maven
mvn clean package -DskipTests
Run the Application: Once the project is built (or if it's already built), you can run the Spring Boot application using the java command:
java -jar target/your-project-name.jar
Replace your-project-name.jar with the actual name of your JAR file generated by Maven or Gradle.
Here is the appearance when it runs successfully.
Try to open https://localhost:8080/ , and you should see this page.
Try to click documentation link and you should see Swagger page
Congratulations! You've successfully completed this tutorial. By now, you should have a solid understanding of how to build a simple CRUD application using Spring Boot for the backend. We hope this guide has provided you with valuable insights and practical skills to apply in your projects.
Thank you for following along, and happy coding! If you have any questions or need further assistance, feel free to reach out. Keep exploring and building amazing applications!
You can check out my repository for this project here . Feel free to clone it, explore the code, and use it as a reference for your own projects.
Stay tuned for our next article, where we will dive into Jasper Reports for dynamic report generation. We'll guide you through setting up Jasper Reports in your project, designing report templates, and integrating them with your data sources. See you in the next tutorial!