Adding Product
Start course
2h 34m

In this course, we will learn the concepts of microservice and spring framework with a focus on Spring Boot.

Learning Objectives

  • Understanding Spring Boot

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello, dear friends. In this video, we will start working on product operations starting with the adding product. Let's begin. First, I want to start by adding controllers for admin and employee. Add a new package named controller. Or if you want you can use the web. Now, add a new class and name it AdminController. Now, we need a controller annotation. Also, we need a RequestMapping annotation to change the path. Path will be admin, done. Let's add another controller for the employee. Add a new class and name it EmployeeController. Add controller annotation, RequestMapping will be an employee. Done. Now, we can define our repositories for product. Add new interface named ProductRepository extends JpaRepository. Class must be product and the ID variable type is long. We need a repository annotation. Done. Now, let's add product service. Add a new interface in the service folder named ProductService.

Now, in this interface, we have to define our CRUD methods for product. First one is getAllProducts, saveProduct, getProductById, deleteProduct. That'll be enough for us. Now, let's add an implementation service for a product. Add a new class and name it ProductServiceImplementation, implements ProductService, Now, we will override all methods. First inject the ProductRepository. Now, I want to start with save product. This will take the product entity as a parameter. Only we need to call the save method of the product repository. All right, this will be enough for the time being because we will just be performing addProduct operations in this video. In the following videos, I will add more operations to each process. So, let's create the AddProduct page.

Add a new HTML page named AddProduct. First, I will add thyme leaf libraries. Now, let's define the layout. Add title. Now, with the content fragment, we can merge this page with the admin layout. Done. Now, we can design our page. Add form. I'll use the save product action which will define in the AdminController. Object will be product.

Let's add elements. First, a label for the product name. Now, add an input for the product name. The field will be name. Another label for price. Add another input for the product price. The field will be price. Copy for stock. The field is stock. Now, we also need a button. Okay, done. Now, in the AdminController, we need a method to access this page.

Open AdminController Now, we need an AddProduct method. Mapping will be AddProduct. Model will be parameter. Now, define an instance from product entity. model addAttributes product product. Now, return AddProduct. That will be enough for us. Let's add a product list page. Because after adding we have to redirect the page to the list page but for now, I will only add a page. In the next video, we will show the products on list. Add new HTML named ProductList. I won't add any item for now. Go back to the AdminController. First, add a new method named product list. The model will be model. We need to get mapping annotation.

The path will be a ProductList. For now, I will only redirect the page to the ProductList. Now, let's do an add operation. Add a new method named SaveProduct. Add mapping. SaveProduct will be the path. This action is already configured on the SaveProduct page. Inject ProductService. Now, we will get the model from the form, so we need the ModelAttribute annotation. The name will be product. Be careful, this product must be the same with the object name of product HTML page, got to remember that. Now, in method, only we need to call to saveProduct method with a product. After saving, we will redirect the page to the product list page. That's enough. The last operation is to change the action of the menu item in the admin layout. Look, we've already defined our sub menu for add product. We only have to redirect to the AddProduct page. Okay, everything is ready, let's try. Run project.

Open browser, Log in with admin user. Now, go to the Add Product page. Fill items. I want to add items for computers. First one is excalibur, price, stock, 'Save.' Look now we're on the list page, let's add another. Second one is Lenovo. Now, we finish saving product operations. So, that's enough for this video. In the next video, we will continue showing these products on the products list page, so I'll see you there.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.