Spring-Rest-DTOPattern

In this Example I used DTO Pattern between Presentation Layer and Business Layer. When We want to use CRUD operations we dont want to use directly our model Class so We created the DTO object. Because we dont want to show to user all infos about entity. We want to secure some info fields like password,email,another things. Business Layer and Persistence Layer interat with Entity Object Some annotations here I want to talk about @Repository @Service @RestController,@Entity @Repository it says to us that class interact with database with SQL Querys (JpaRepository facilitates to us ). @Service it says to us that is service class.When we build a program .It creates a object of this class. @RestController Controller says to us what is our endpoint what is our directions inside of API (It includes best practices thats important) Rest Controller works with JSON and XML Object it facilitates to us .We can send to server our Objects and get it. @Entity class says to us that class is a table in our database .

image

What is DTO Pattern Above I said what is it but I want to talk about Spring IOC Container and Dependency Injection and another important Annotations.

image here We can say When we run a program Service annotations creates a instance of service class and ıt stores inside of Spring IOC Container When we call it inside of another class We can use @Autowired annotations it shows to us that is dependency Injection Spring Ioc container calls automatically tht object to here inside of Controller class from Service class.

image

Here We can see Component and Bean anonotations They basically create an instance and it sends to Spring Ioc container .And Whenever we want to use ,We can use easily @Bean annotations is based method .We can call it anywhere from our Program and use it.

okey lets build up the puzzle! When We use DTO pattern We must use ModelMapper it maps to for example User -> UserDTO that is a bean we can do it