Skip to content

ImanNazirah/demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot Project

Java RESTful API with Spring Boot

This is a demo Spring Boot application that uses the following technologies:

• MySQL: For database management.

• Default Cache (ConcurrentHashMap): This application uses ConcurrentHashMap for in-memory caching, which is only suitable for local development or small-scale testing. For production environments, it is recommended to replace the default cache with a more scalable, fault-tolerant caching solution such as Redis or Hazelcast.

• JWT Token: For securing APIs with authentication and authorization.

Usage

Branch feature/unrestricted-api(No Header Authorization)

In this branch, API calls can be made without including any authentication headers. For example, you can use tools like Postman or cURL without setting an Authorization header. This is intended for quick development or testing scenarios where authentication is not needed.

Branch master (Header Authorization Required)

In this branch, every API request must include the correct authorization token in the header. For example:

Authorization: Bearer <your_token>

Without the proper authorization header, the server will respond with an error (e.g., HTTP 401 Unauthorized). This configuration is more secure and represents a scenario where authentication and authorization are required to access the resources.

Environment Setup

Create DB & TABLE

  1. Create databse into your MySQL server

$ CREATE DATABASE api;

  1. Then go to file demo.sql , to create table and insert data

Steps to run application

Step 1: Open application.properties and inject your variable.

Step 2: Run application by using Maven wrapper

  • UNIX

$ ./mvnw clean install -DskipTests

$ ./mvnw spring-boot:run

  • Windows

$ mvnw.cmd clean install -DskipTests

$ mvnw.cmd spring-boot:run

Step 3: Test the application

Master branch:

  • SET TOKEN :

TOKEN=$(curl -s -X POST http://localhost:8080/api/user/login
-H "Content-Type: application/json"
-d '{"username": "demo_test1", "password": "mypassword"}'
-i | grep -i "Authorization:" | sed 's/Authorization: Bearer //')

  • GET ALL:

curl -X GET http://localhost:8080/api/spotify
-H "Authorization: Bearer $TOKEN"
-H "Content-Type: application/json"

  • GET BY ID :

curl -X GET http://localhost:8080/api/spotify/
-H "Authorization: Bearer $TOKEN"
-H "Content-Type: application/json"

  • POST

curl -X POST http://localhost:8080/api/spotify
-H "Content-Type: application/json"
-H "Authorization: Bearer $TOKEN"
-d '{ "trackName": "", "artistName": "", "genre": "", "popularity": }'

  • PUT :

curl -X PUT http://localhost:8080/api/spotify/
-H "Content-Type: application/json"
-H "Authorization: Bearer $TOKEN"
-d '{ "trackName": "", "artistName": "", "genre": "", "popularity": }'

  • DELETE

curl -X DELETE http://localhost:8080/api/spotify/
-H "Content-Type: application/json"
-H "Authorization: Bearer $TOKEN"

  • GET BY QUERY (available query parameter = 'trackName', 'artistName', 'genre','popularity','page', 'pageSize')

curl -X GET "http://localhost:8080/api/spotify/search?genre="
-H "Content-Type: application/json"
-H "Authorization: Bearer $TOKEN"

  • GET GLOBAL SEARCH (available query parameter = 'searchText', 'column', 'page', 'pageSize'; where value for column are ['trackName', 'artistName', 'genre','popularity'])

curl -X GET "http://localhost:8080/api/spotify/global-search?searchText=&column=artistName&column=genre"
-H "Content-Type: application/json"
-H "Authorization: Bearer $TOKEN"

  • LOGOUT :

curl -X POST http://localhost:8080/api/user/logout
-H "Authorization: Bearer $TOKEN"

References

About

Simple Restful Api Spring Boot Java.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors