Spring Boot 002: A Simple Model with Faker

tahaburak.koc
3 min readNov 24, 2019

Previous article: Spring Boot 001: Set up and Ngrok

Hello. I’ve decided to continue the project from previous article by creating a data model and make use of a library called “Faker” with it. You can just clone it to your local device and follow up the rest.

1. Structure

So what are we actually going to build? Well, a model for sure. But in order to serve this model our project require couple of layers to keep its clean structure.

Basic structure

The image above helps us visualize the structure of our application.

  • Controller layer will receive the requests and send the responses.
  • Service layer will be handling the data according to business logic.
  • Dao layer will be communicating with database.

2. Model Itself — User

Most of the projects requires a user model so I’ll be creating one as follow:

User model

Our model has 4 properties called id, name, surname and birthDate; a constructor and getter/setter methods for each property. Also we have a new package called “model”.

Currently our application doesn’t have any database so I’ll create a dummy service and going to use Faker library with it.

3. Dummy Service and Faker

Faker is a library that generates fake data. It’s very useful on first stages of an application or on a demo application for showcases.

In order to use it we need to add following lines to our pom.xml.

<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.1</version>
</dependency>

Faker provides fake data on lots of categories such as address, name, nation, food, Back To The Future,Harry Potter, Lord Of The Rings ❤️ and also provides these categories with different languages such as en-US, tr, de…

So let’s create our Dummy Service as follows:

Dummy service

As you can see DummyService implements an interface with a simple method called “User getDummyUserById(Long id)”.

Service annotation helps Spring to scan this class and PostConstruct annotation helps to call “fill” method right after construction of this class.

Since our project doesn’t have any database connection we will be calling DummyService from our Dao class:

UserDao

UserService:

UserService

UserController:

UserController

We have created a new controller called “UserController” which handles to request. For this article it has a Get method which takes id and delivers this value to UserService.

4. Conclusion

So lets a have look at final structure of our project:

3 new packages: dao, model, service

A new controller: UserController

A new model: User

A new Dao layer: UserDao

2 new services: DummyService, UserService

Interfaces for services and dao

If you didn’t skip any part of this article you can simply build your project and visit http://localhost:2836/user/id/7 on your browser.

And don’t forget to commit the changes we’ve made.

git add .
git commit -m "implemented user model, dao, service, controller and faker library"

You can find the code at Github.

Sincerely,
Burak.

--

--