Building a RESTful API with Java Spring Boot and JWT Authentication

A Spring Boot RESTful API is a web service built using the Spring Boot framework that follows REST (Representational State Transfer) architectural principles.
It exposes endpoints over HTTP to perform CRUD : Create, Read, Update, Delete operations on resources such as data entities (e.g. models, books, products, items, users ).



Spring Boot uses annotations like @Component@Entity@Repository@Controller@Service .

Main Application

@SpringBootApplication
public class JwtReimbursementApplication {

 public static void main(String[] args) {
  SpringApplication.run(JwtReimbursementApplication.class, args);
  System.out.println("Application Starts..!");
 }
}
Model Class
@Entity
public class ReimbursementModel {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private int id;
 @Column(unique = true)
 private String employeeName;
 int employeeNumber;
 String productType;
 String productName;
 String productProvidor;
 Double amount;
 String submissionDate;
 String comments;
 String isApproved = "pending";
 String approvalDate;
 String approverRemarks;

 int employeeId;

 // Constructors & Getters/Setters

}
Repository
@Repository
public interface ReimbursementRepository extends JpaRepository {

 List findByEmployeeNumber(int employeeNumber);

}
Controller
@RequestMapping("/reimbursement")
@RestController
public class ReimbursementController {

 @Autowired
 ReimbursementService reimbursementService;

 @GetMapping("/list")
 public ResponseEntity get_list() {
  return reimbursementService.get_list();
 }

 @GetMapping(value = "/{employeeNumber}", produces = { "application/json", "application/xml" })
 public ResponseEntity getReimbursement(@PathVariable("employeeNumber") int employeeNumber) {
  return reimbursementService.getReimbursement(employeeNumber);

 }
}
Service
@Service
public class ReimbursementService {

 @Autowired
 ReimbursementRepository reimbursementRepository;

 public ResponseEntity get_list() {
  return new ResponseEntity<>(reimbursementRepository.findAll(), HttpStatus.OK);
 }

 public ResponseEntity getReimbursement(int employeeNumber) {
  List models_list = reimbursementRepository.findByEmployeeNumber(employeeNumber);

  System.out.println(models_list);
  if (models_list.isEmpty())
   return new ResponseEntity<>("no data available", HttpStatus.BAD_REQUEST);
  else
   return new ResponseEntity<>(models_list, HttpStatus.OK);
 }
}

Let’s do a hands-on project : we’ll build a secure RESTful API using Spring Boot and JWT authentication.

A hands-on guide to building a secure user authentication system using Spring Boot, Spring Security and JWT in a RESTful API architecture.

Problem Statement


💼 In this hands-on project, We will build a secure RESTful API using Spring Boot that manages employee reimbursements. The API will allow users to registerlogin using JWT authentication, and submit or retrieve reimbursement requests securely. This tutorial will guide creating endpoints, securing them with JWT and testing them using Postman.

Initialize the database with the following data.

Implement JWT-based authentication and authorization in Spring Boot application. The JWT token should carry the user’s role either EMPLOYEE or SUPERVISOR — so that the role can be extracted and used for access control.

The token must be included in the Authorization header of each secured request, using the Bearer scheme.

Authorization: Bearer <JWT_TOKEN>



API Output & Snapshots

Our Helpers For Reference….


GET API : 
/public


POST : /login







POST : /reimbursement/add











GET : /reimbursement/{employeeNumber}



PATCH : /reimbursement/update/{id}





DELETE : /reimbursement/delete/{id}






GET : /v3/api-docs

All Test Cases Passed : )

Thanks !
Happy Reading 😇

Press enter or click to view image in full size

Comments

Popular posts from this blog

The Perspective !

ABAP | Advanced Business Application Programming | Dynamic Programming

True Education Through My Lens !