Agenda
In this blog, we are going to talk about the following points:-
-
What is Spring Boot?
- Spring Boot Application Architecture
-
Why Spring Boot?
- Some important Annotation of Spring boot
-
Let’s set up a Spring Boot application
-
Some Important Dependency for Spring Boot Project
-
Entity
-
Repository
-
Service
-
Controller
-
Helperย
-
Constant
-
Router
What is Spring Boot?
Spring Boot is an open-source, microservice-based Java web framework. The Spring Boot framework creates a fully production-ready environment that is completely configurable using its prebuilt code within its codebase.
Spring Boot Application Architecture
Why Should You Use Spring Boot?
- Fast and easy development of Spring-based applications
- No need for the deployment of war files
- The ability to create standalone applications
- Helping to directly embed Tomcat, Jetty, or Undertow into an application
- No need for XML configuration
- Reduced amounts of source code
- Additional out-of-the-box functionality
- Easy start
- Simple setup and management
- Large community and many training programs to facilitate the familiarization period
Some Important Annotations of Spring Boot
Spring Boot Application and Perform CRUD Operation
Step 1: Open Spring Initializr http://start.spring.io.
Step 2: Select the Spring Boot version 2.3.0.M1.
Step 2: Provide the Group name. We have provided com.demo.
Step 3: Provide the Artifact Id. We have provided spring-boot-crud-operation.
Step 5: Add the dependencies Spring Web, Spring Data JPA
Step 6: Click on the Generate button. When we click on the Generate button, it wraps the specifications in a Jar file and downloads it to the local system.
Dependencies for Spring Boot Project
// Spring boot validation
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
//Jwt Token Security
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
//Json Dependency
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>
spring-boot-starter-web -> Services on Tomcat - typically REST services using Spring MVC for web layer
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web-services -> SOAP services
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
spring-boot-starter-jersey -> Services on Tomcat - typically REST services using Jersey implementation of JAX-RS for web layer
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
MySQL Connector Java Maven Dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
//Spring boot swagger Generator
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.3</version>
</dependency>
//Dependency for setter getter and constructors
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
//Spring boot Jpa Dependency (ORM)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Read Also – Difference Between CodeIgniter 3 and CodeIgniter 4 versions
Create The Blow Class in Your Project

Entity Class โ Book.java
package com.example.msn.Entities;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String Qty;
private Timestamp createdOn;
}
Repository Class โ BookRepository.java
package com.example.msn.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.msn.Entities.Book;
@Repository
public interface BookRepository extends JpaRepository<Book,Integer> {
}
Service Class โ BookService.java
packagecom.example.msn.Services;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.msn.Entities.Book;
import com.example.msn.Repository.BookRepository;
@Service
publicclassBookService {
@Autowired
ย ย ย ย private BookRepository bookRepository;
ย ย ย ย public Void deleteBook(Integer id) {
ย ย ย ย ย ย ย ย bookRepository.deleteById(id);
ย ย ย ย ย ย ย ย return null;
ย ย ย ย }
ย ย ย ย public List<Book> getBooks() {
ย ย ย ย ย ย ย ย List<Book> books = bookRepository.findAll();
ย ย ย ย ย ย ย ย return books;
ย ย ย ย }
ย ย ย ย public Book addBook(Book book1) {
ย ย ย ย ย ย ย ย Book book = bookRepository.save(book1);
ย ย ย ย ย ย ย ย return book;
ย ย ย ย }
ย ย ย ย publicย Optional<Book> findById(Integer id) {
ย ย ย ย ย ย ย ย Optional<Book> book1 = bookRepository.findById(id);
ย ย ย ย ย ย ย ย return book1;
ย ย ย ย }
}
Controller โ BookController.java
package com.example.msn.Controllers;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import com.example.msn.Constants.ResMesConstants;
import com.example.msn.Entities.Book;
import com.example.msn.Helpers.ApiResponse;
import com.example.msn.Requests.AddBookRequest;
import com.example.msn.Requests.EditBookRequest;
import com.example.msn.Services.BookService;
@Controller
public class BookController {
ย ย ย @Autowired
ย ย ย private BookService bookService;
ย ย ย public ResponseEntity<Object> deleteBook(Integer id) {
ย ย ย ย ย ย ย bookService.deleteBook(id);
ย ย ย ย ย ย ย ApiResponse apiResponse = new ApiResponse(false, ResMesConstants.BOOK_DELETED_SUCCESSFULLY,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย HttpStatus.OK,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย null);
ย ย ย ย ย ย ย return new ResponseEntity<Object>(apiResponse, HttpStatus.OK);
ย ย ย }
ย ย ย public ResponseEntity<Object> getBooks() {
ย ย ย ย ย ย ย List<Book> book = bookService.getBooks();
ย ย ย ย ย ย ย ApiResponse apiResponse = new ApiResponse(false, ResMesConstants.RECORD_FOUND_SUCCESSFULLY,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย HttpStatus.OK,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย book);
ย ย ย ย ย ย ย return new ResponseEntity<Object>(apiResponse, HttpStatus.OK);
ย ย ย }
ย ย ย public ResponseEntity<Object> editBook(EditBookRequest editBookRequest) {
ย ย ย ย ย ย ย Optional<Book> book1 = bookService.findById(editBookRequest.getId());
ย ย ย ย ย ย ย if (book1.get() == null) {
ย ย ย ย ย ย ย ย ย ย ย ApiResponse apiResponse = new ApiResponse(false, ResMesConstants.SOMETHING_WENT_WRONG,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย HttpStatus.PRECONDITION_FAILED,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย null);
ย ย ย ย ย ย ย ย ย ย ย return new ResponseEntity<Object>(apiResponse, HttpStatus.PRECONDITION_FAILED);
ย ย ย ย ย ย ย }
ย ย ย ย ย ย ย book1.get().setName(editBookRequest.getBookname());
ย ย ย ย ย ย ย book1.get().setQty(editBookRequest.getQty());
ย ย ย ย ย ย ย Book book = bookService.addBook(book1.get());
ย ย ย ย ย ย ย if (book == null) {
ย ย ย ย ย ย ย ย ย ย ย ApiResponse apiResponse = new ApiResponse(false, ResMesConstants.SOMETHING_WENT_WRONG,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย HttpStatus.PRECONDITION_FAILED,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย null);
ย ย ย ย ย ย ย ย ย ย ย return new ResponseEntity<Object>(apiResponse, HttpStatus.PRECONDITION_FAILED);
ย ย ย ย ย ย ย }
ย ย ย ย ย ย ย ApiResponse apiResponse = new ApiResponse(false, ResMesConstants.BOOK_UPDATED_SUCCESSFULLY, HttpStatus.OK,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย null);
ย ย ย ย ย ย ย return new ResponseEntity<Object>(apiResponse, HttpStatus.OK);
ย ย ย }
ย ย ย public ResponseEntity<Object> addBook(AddBookRequest addBookRequest) {
ย ย ย ย ย ย ย Book book1 = new Book();
ย ย ย ย ย ย ย book1.setName(addBookRequest.getBookname());
ย ย ย ย ย ย ย book1.setQty(addBookRequest.getQty());
ย ย ย ย ย ย ย Book book = bookService.addBook(book1);
ย ย ย ย ย ย ย ApiResponse apiResponse = new ApiResponse(false, ResMesConstants.BOOK_ADDED_SUCCESSFULLY, HttpStatus.OK,
ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย book);
ย ย ย ย ย ย ย return new ResponseEntity<Object>(apiResponse, HttpStatus.OK);
ย ย ย }
}
Helper โ ApiResponse.java
package com.example.msn.Helpers;
import org.springframework.http.HttpStatus;
import lombok.Data;
@Data
public class ApiResponse {
ย ย ย private boolean error;
ย ย ย private String message;
ย ย ย private HttpStatus HttpStatus;
ย ย ย private Object data;
ย ย ย public ApiResponse(boolean error, String message, HttpStatus httpStatus, Object data) {
ย ย ย ย ย ย ย this.error = error;
ย ย ย ย ย ย ย this.message = message;
ย ย ย ย ย ย ย this.HttpStatus = httpStatus;
ย ย ย ย ย ย ย this.data = data;
ย ย ย }
}
Constants Constant.java
package com.example.msn.Constants;
public interface ResMesConstants {
ย ย ย public final String RECORD_FOUND_SUCCESSFULLY = "Record found Successfully";
ย ย ย public final String RECORD_NOT_FOUND = "Record not found";
ย ย ย public final String INVALID_TOKEN = "Invalid token";
ย ย ย public final String INVALID_REQUEST = "Invalid request";
ย ย ย public final String EMAIL_IS_ALREADY_REGISTERED = null;
ย ย ย public final String BOOK_DELETED_SUCCESSFULLY = null;
ย ย ย public final String BOOK_UPDATED_SUCCESSFULLY = null;
ย ย ย public final String BOOK_ADDED_SUCCESSFULLY = null;
ย ย ย public final String SOMETHING_WENT_WRONG = "something went wrong!";
ย ย ย public final String LOGIN_SUCCESSFULLY = "Login successfully";
ย ย ย public final String INVALID_USER_PASSWORD = "Invalid user password";
ย ย ย public final String PRECONDITION_FAILED = "Precondition Failed";
ย ย ย public final String MOBILE_NO_LENGTH_MUST_9 = "Mobile no length must >9";
ย ย ย public final String USER_NAME_CAN_NOT_BE_EMPTY = "User Name Can not be empty";
ย ย ย public final String PASSWORD_LENGTH_MUST_5 = "Password length must >5 ";
ย ย ย public final String MOBILE_NO_IS_ALREADY_REGISTERED = "Mobile no is already registered!";
ย ย ย public final String REGISTRATION_SUCCESSFULLY = "Registration successfully ";
ย ย ย public final String INVALID_MOBILE_NO = "Invalid Mobile No ";
}
Read Also- ย Install The Telescope In Laravel
Router openApi.java
package com.example.msn.Routes;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.msn.Controllers.BookController;
import com.example.msn.Requests.AddBookRequest;
import com.example.msn.Requests.EditBookRequest;
import com.example.msn.Requests.LoginEntityRequest;
import com.example.msn.Requests.RegistrationRequest;
@RestController
public class OpenApi {
ย ย ย @Autowired
ย ย ย private BookController bookController;
ย ย ย // addBook api
ย ย ย @PostMapping(path = "/addBook", consumes = "application/json")
ย ย ย public ResponseEntity<Object> addBook(@RequestBodyย AddBookRequest addBookRequest) {
ย ย ย ย ย ย ย return bookController.addBook(addBookRequest);
ย ย ย }
ย ย ย // editBook api
ย ย ย @PostMapping(path = "/editBook", consumes = "application/json")
ย ย ย public ResponseEntity<Object> editBook(@RequestBody EditBookRequest editBookRequest) {
ย ย ย ย ย ย ย return bookController.editBook(editBookRequest);
ย ย ย }
ย ย ย // getBooks api
ย ย ย @GetMapping(path = "/getBooks")
ย ย ย public ResponseEntity<Object> getBooks() {
ย ย ย ย ย ย ย return bookController.getBooks();
ย ย ย }
ย ย ย // deleteBook api
ย ย ย @PostMapping(path = "/deleteBook/{id}")
ย ย ย public ResponseEntity<Object> deleteBook(@PathVariable(name = "id" ) String id) {
ย ย ย ย ย ย ย return bookController.deleteBook(Integer.valueOf(id));
ย ย ย }
}



