Let’s Develop an E-Commerce Application From Scratch Using Java and Spring
By: Umar Draz
Tags: Spring Boot, Java, Swagger

About Spring Framework

The Spring Framework is a major open-source Java/J2EE application development framework for more productive application development. It is the most popular Java framework with a 30% share of usage. The Spring Framework features enable efficient development from simple Web to complex enterprise applications.

The main concepts that the Spring Framework depends on are:

Dependency Injection (DI) Aspect-Oriented Programming (AOP) Java Persistence API (JPA)

Prerequisites

Knowledge in Core Java Coding skills Knowledge in Servlet and JSP Java Development Kit (JDK) IntelliJ IDEA Ultimate — open-source (recommended) MySQL database — You can get it here! A good browser (Chrome — recommended)

Make sure you have all the listed prerequisites software installed and have basic knowledge of the request-response cycle as well as web containers. What more? Let’s jump into the tutorial!

Project Setup

Open Spring Tool Suite application

Navigate to your workspace

Click File -> New -> Project ->Spring Starter Project

Give the essential data in the opened dialog box

Dependencies

The dependencies I am adding to this project are given below. You can either add them during the creation of the project itself or later search for them in the Maven repository and add the tags of the required version in the pom.xml file.

Spring boot starter web

Tomcat embed jasper

Spring boot starter Data JPA

Mysql connector java

Spring boot starter test

Swagger 2

Swagger UI

Project Hierarchy

The hierarchy is the important thing to notice in the Spring boot Application design structure. My project hierarchy is as below.

Overview of our Backend Application

In this Spring Application following are important packages that you have to know before starting.

Structure of Java Spring service

├── common

├── config

├── controller

├── exception

├── model

└── service

Details about packages and classes the contain

common

In this package, there are two classes

· ApiResponse.java

This class provides the status of API response. It has three methods is Success (), get Message (), get Timestamp (). The get Timestamp () method returns the Current Date and Time in String format.

PagedList.java

This class contains all the information about the pages in the List data structure and provides total Pages, total Elements, has Next, has Previous attributes getter and setter.

2. config

In these packages, there are two sub-packages named documentation and security.

· Documentation

In this Package, it provides information about the product and API.

Product API method provides information about the product and gets the Api Info method return information about Api like contact, Title, version, license.

· Security

This class is very important for applications because it provides the basic security for the whole application over HTTP or HTTPS protocol. It also implements configuration for users that which users have access to which pages or repositories.

3.controller

The User Controller class provides two HTTP methods GET and Post. The Get mapping function return a complete list of Users and the Post Mapping Function saves the new User profile in the Database.

4. exception

This class is used in case of exception in any stage of the application. It has two classes Exception handler and validation exception, which throws when there is a validation error in the application at run time.

ExceptionHandlerAdvice.java

ValidationException.java

5. model

The model class is a mirror of the user_profile table in the database in form of a java object. All attributes can be accessed through this class. You can also update the attribute values using the model class, which also makes changes in a database table.

6. service

As we know the spring boot framework follow the POJO model and every controller has its own service interface, which provides the methods / Operation that is performed in the application.

In service class, there are only two methods list Profiles and add Profiles, which provide information. We can extend or add more functionality in the future according to requirements.

EcommerceApplication.java is the main class of applications.

Application. properties

You can define the properties of the application in this property file like if you want to change the default port of tomcat running to 8182 instead of 8080 you just add a line in this file as below server.port=8182