Tuesday, February 20, 2018

First Rest API Application - Top Spring Framework Interview Questions

First Rest API Application using Spring Rest and Springboot?

What is SpringBoot?

Objective : To build an application of User Creation using Rest Services with Spring boot.

We will first create the Spring boot project usingstart.spring.io (see below image)
Now we will download the project and extract and import it into eclipse.
See the Project Hierarachy after all the package created.
Now we need to add below class
UserController.java – It will have all the Endpoints defined for External





JsonUser.java – This is a DTO class


User.java – This is the Entity Class 


UserRepository.java – This is the CRUDRespository interface for User Operations.
IUserService.java-  This is the user service Interface having all the methods signature for User Operations.

UserService.java – This is the IUserService implementated Class having methods Implementation of User Operations.

For Database as per simplicity we are use H2 Database(it is embedded database )

Explanation of Classes

UserController.java
@RestController is an annotation used to make the Class as a Rest Controller for Spring to identify.
@Requestmapping(<arg>) is annotation is used to provide the identity path for the URL  of this controller.

How to connect database using JDBCTEMPLATE

Arg – the path of the controller

@Autowired
       UserService userService; It will inject the Service class in Controller Class

@PostMapping
       public ResponseEntity<JsonUser> create(@RequestBody @Valid JsonUser user ,BindingResult result ,HttpServletRequest request){
             
              if(user== null)
                     return ResponseEntity.badRequest().build();
               return ResponseEntity.ok().body(userService.createUser(user));
             
       }

@PostMapping is showing that this method will be taking Post type of calls where the Request Body will be of type JsonUser.
After that we are checking the user is not null ,if it is we will return bad request error.
If not , we will call the service call createuser method with jsonUser object as a argument.

JsonUser.java
This class is a DTO class

User.java
This is Entity Class which will be persisted in Database.

@Entity is used to say that the class is entity class for hibernate.
@Table is used to define the table name for This class , if the table name is same as the class name we don’t need to difine the name otherwise it will become @Table(name=”mytable”)
@Id is to as this is identity attribute of the table which @GeneratedValue to show how the id will be automatically generated.
@Column is used to defined the Column Attributes.

UserRespository.java
This is Interface which implementes JpaRespository<User,Serializable> that means , It provide all the Db Operations function like save, findAll etc


IUserService.java
This is Service Interface having all the User Operations like createUser, getUser, updateUser, deleteUser

UserService.java
This is Service Implementation of IUserService where we are implementing the logic of creating the user using Respository Functionality.


Testing of Application Using Rest Client
Now we need to run the Application and then we will use RestClient like RestLetClient to test the API






Congratulations have creating our first Rest Service Application Using Spring framework.


If you like this article please share it with your friend or colleagues.

Thanks for reading
Noeik 


0 comments:

Post a Comment