Skip to content

Implements a JWT protected, documented and cached REST API, using Spring Security, OpenAPI and Redis respectively. Manually creates database using mySQL.

Notifications You must be signed in to change notification settings

bodoque-01/JWT-Protected-REST-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API

Java Version Spring Boot Version MySQL Connector OpenCSV Lombok JUnit Mockito Redis springdoc-openapi

A simple CRUD (Create, Read, Update, Delete) API project for managing employee records. App documentation

Description

This project provides a RESTful API for basic CRUD operations on an employee database. It leverages the following technologies:

  • Spring Boot for building robust and scalable applications.
  • Spring Data JPA for easy data access with Hibernate.
  • MySQL for data storage and retrieval.
  • OpenCSV for CSV parsing.
  • Project Lombok for reducing boilerplate code in Java.
  • JUnit for unit testing.
  • Mockito for mocking objects in tests.
  • Redis for caching and improving requests performance.
  • OpenApi for documentation generation.

How to Run

  1. Ensure you have Java 17, Maven and Redis (for caching) installed.
  2. Clone this repository.
  3. Configure your MySQL database details in the application.properties file.
    • By default, the database is set to localhost:3036 with root as both the username and password. The database should be created using the sql script inside the "sql-scripts" folder.
  4. Go to the repository's location
  5. Run Redis server
    • Open a terminal and run the following command to start the Redis server. You can change the port Redis will use in application.properties. By default, it's 6484.
      redis-server --port 6484
      Or add the following to the application.properties file to disable cache.
    • spring.cache.type=none
  6. Run the application using Maven:
    mvn spring-boot:run
  7. Get your token by making a POST request to http://localhost:8080/token with with basic authentication using the username "eren" and password "password". You can use tools like cURL or Postman.

Note on security

The JWT implementation in this project is for educational purposes only. The user "eren" is an in-memory user, and the public and private keys are exposed for demonstration purposes. In a production environment, secure practices should be followed for user authentication and key management.

Usage

This is important! While the following documentation is still right, you can better read (and interact) with the swagger documentation for the API at http://localhost:8080/swagger-ui/index.html

The API adheres to REST principles, making it intuitive to interact with. For example, to retrieve information about an employee with ID 1, simply make a GET request to:

GET localhost:8080/employee/1

For updating or deleting an employee with a specific ID (employee_id), use the corresponding request to:

PUT or DELETE localhost:8080/employees/{employee_id}

When updating, remember to include the employee details in JSON format, specifying the firstName, lastName, and email properties.

To interact with the API, you can use external tools like Postman for POST, PUT, and DELETE requests. Alternatively, for a more user-friendly experience, consider using the version available in crudMVCDemo. It's an MVC CRUD web app that provides a graphical UI for adding, deleting, or updating employees, yet this version is more developed.

If you want to customize the number of employees displayed or control the page size when retrieving all employees (GET URL /employees), you can use query parameters. For example:

GET http://localhost:8080/employees?size=3

By default, the page size is set to 10 and cannot exceed 100. Feel free to adjust the query parameters according to your preferences.

About

Implements a JWT protected, documented and cached REST API, using Spring Security, OpenAPI and Redis respectively. Manually creates database using mySQL.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages