# Let’s develop an Ecommerce Application from Scratch using Java and Spring


Let’s Develop an E-Commerce Application From Scratch Using Java and Spring

We are going to build an e-commerce application using Java, Spring backend, build web UI in Vue.js, and mobile UI using android. Stay tuned!

![Photo by [Mark König](https://cdn.hashnode.com/res/hashnode/image/upload/v1610393895755/aosEubyNP.html) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/7680/0*KZwhzPdpevZ4d5Sg)*Photo by [Mark König](https://unsplash.com/@markkoenig?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)*

Every E-Commerce platform needs users, so in the first tutorial, we are going to create and display user-profiles. We are going to first build the backend APIs using Java and Springboot, then we are going to create UI in Vue.js and android mobile app.

## Live Demo

You can test the API at the following swagger link. You can create and list all the users.
[**Swagger UI**
*Edit description*remotedevs.org](http://remotedevs.org:8080/api/swagger-ui.html)

## 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:

1. Dependency Injection (DI)

1. Aspect-Oriented Programming (AOP)

1. Java Persistence API (JPA)

## Prerequisites

1. Knowledge in Core Java

1. Coding skills

1. Knowledge in Servlet and JSP

1. Java Development Kit ([JDK](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html))

1. [IntelliJ IDEA Ultimate](https://www.jetbrains.com/idea/download/) — open-source (recommended)

1. MySQL database — You can get it [here](https://dev.mysql.com/downloads/workbench/)!

1. 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 -&gt; New -&gt; Project -&gt;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](https://mvnrepository.com/) 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1610393897025/sZh94JAx8.png)

## Overview of our Backend Application

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

This is the spring architecture. The outside world calls the REST Apis, which interacts with the Service. Service calls the repository. The repository interacts with the database. We follow this pattern to make the codebase maintainable, instead of having spaghetti code which can be a nightmare in long term.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1610393898414/55ZfLI6CB.jpeg)

Let's look at first Rest controllers

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

As we can see UserControllers has a reference to UserService.

## Service

As we know the spring boot framework follows 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.


Service calls UserProfileRepository which interacts with the database in form of models. Let's have a look at UserModel.

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


## Result

After this save the file and run the application. Now you tomcat is listening on 8182 port.
[**Swagger UI**
*Edit description*remotedevs.org](http://remotedevs.org:8080/api/swagger-ui.html#/user-profile-controller)

## What is Swagger

Swagger is an Interface Description Language for describing RESTful APIs expressed using JSON. Swagger is used together with a set of open-source software tools to design, build, document, and use RESTful web services. Swagger includes automated documentation, code generation, and test-case generation.

You can access the application documentation by entering the following URL in your browser.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1610393900386/qLG0_eUB5.png)

You can edit your API information in documentation.swaggerConfig.java class and getApiInfo() method.

Swagger is very useful on the developer side because it provides very user-friendly API information as well as provide a way to test the API.


Now we are going to look at some extra classes, which play an important role too.

## **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.

## C**onfig**

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.


## E**xception**

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
```



## *Hope you had a great session with us! Follow us for such great and awesome tutorials!*

Follow the next tutorial where we will build UI in both Vue.js and Android.
[**Let’s Create a Frontend for our eCommerce App with Vue.js**
*We are building a complete E-commerce application from scratch. Today we are going to add the Products feature.*medium.com](https://medium.com/javascript-in-plain-english/lets-create-vue-js-frontend-for-products-in-our-e-commerce-app-14cc8c3459c8)
[**Android UI for E-Commerce User Profile backend**
*We are going to create an android UI for our UserProfile backend API we are creating for our demo E-commerce tutorial*medium.com](https://medium.com/technology-hits/android-ui-for-e-commerce-user-profile-backend-4053ab122a13)

## Happy learning! : )

## Resources
[**Android UI for E-Commerce User Profile backend**
*We are going to create an android UI for our UserProfile backend API we are creating for our demo E-commerce tutorial*medium.com](https://medium.com/technology-hits/android-ui-for-e-commerce-user-profile-backend-4053ab122a13)
[**A Simple Site Made With Spring MVC Architecture**
*Nowadays, developers tend to move towards frameworks than using the actual Language itself for development. The reason…*medium.com](https://medium.com/nerd-for-tech/a-simple-site-made-with-spring-mvc-architecture-ac8e283cf7e4)
[**A Simple User Authentication API made with Spring Boot**
*These days its easier than ever to make a proper application that can do what you need it to do. There are thousands of…*medium.com](https://medium.com/webtutsplus/a-simple-user-authentication-api-made-with-spring-boot-4a7135ff1eca)
