Spring Boot REST API for managing hotels with MySQL persistence, JWT authentication, and rating-service integration via RestTemplate.
This project demonstrates a compact Spring Boot REST application for hotel data management. It uses repository-backed JPA persistence, JWT-based stateless authentication, public user registration, and role-protected hotel endpoints. In this version, hotel lookup by ID enriches the hotel rating by calling an external rating service.
- Spring Boot REST API setup
- Spring Data JPA repository pattern
- MySQL-backed persistence
- Spring Security with JWT authentication (stateless)
- Method-level authorization with
@PreAuthorize - User registration and token-based login flow
RestTemplatecommunicator pattern for calling an external rating serviceGETendpoint for retrieving a hotel by ID (rating fetched from rating service)GETendpoint for listing all hotelsPOSTendpoint for creating a hotel recordDELETEendpoint for deleting a hotel recordGETendpoint for listing registered users
- Java 17
- Spring Boot 2.7
- Spring Web
- Spring Data JPA
- Spring Security
- Spring Validation
- MySQL
- Maven
- Lombok
- JJWT
hotel/
├── CHANGELOG.md
├── README.md
├── pom.xml
├── mvnw
├── mvnw.cmd
└── src/
└── main/
├── java/com/cn/hotel/
│ ├── communicator/
│ ├── config/
│ ├── controller/
│ ├── dto/
│ ├── exceptions/
│ ├── jwt/
│ ├── model/
│ ├── repository/
│ ├── security/
│ ├── service/
│ └── HotelApplication.java
└── resources/
└── application.yml
- Open a terminal in the project root.
- Update the MySQL connection values in
src/main/resources/application.ymlif needed. - Ensure the rating service is available at
http://localhost:8081. - Run
mvn test. - Run
mvn spring-boot:run. - Register a user with
POST /user/register. - Obtain a token with
POST /auth/login. - Call protected endpoints with
Authorization: Bearer <token>.
Available endpoints:
POST /auth/loginGET /userPOST /user/registerPOST /hotel/createGET /hotel/id/{id}GET /hotel/getAllDELETE /hotel/remove/id/{id}
Notes:
/user/registerand/auth/loginare public.GET /hotel/id/{id}expects theAuthorizationheader and forwards the JWT to the rating service.- Roles are enforced with
@PreAuthorize(ADMINfor create/list/delete,NORMALfor get-by-id).
Example request body for user registration:
{
"username": "john",
"password": "john123"
}Example request body for login:
{
"username": "john",
"password": "john123"
}Example request body for hotel creation:
{
"name": "Sea View Inn",
"rating": 8,
"city": "Goa"
}- Suggested repository description:
Spring Boot REST API for hotel record management with MySQL persistence, JWT authentication, and rating-service integration via RestTemplate. - Suggested topics:
java,java-17,spring-boot,spring-security,spring-data-jpa,mysql,rest-api,hotel-management,jwt,resttemplate,microservices,maven,learning-project,portfolio-project