Hackforge Academy

Category: java

booking app in spring boot

Published on 22 Apr 2026

Explanation

Create a Booking entity class to represent booking details such as id, name, and date.

Code:

@Entity
public class Booking {

  @Id
  @GeneratedValue(strategy = 
GenerationType.IDENTITY)
  private Long id;

  private String customerName;
  private String bookingDate;

  // getters and setters
}

Explanation

Create a JPA repository interface to interact with the database without writing SQL queries.

Code:

public interface BookingRepository 
extends JpaRepository<Booking, Long> {
}

Explanation

Create a service class to handle business logic such as saving and retrieving bookings.

Code:

@Service
public class BookingService {

  @Autowired
  private BookingRepository
 bookingRepository;

  public Booking saveBooking(
Booking booking) {
    return bookingRepository.save(booking);
  }

  public List<Booking> getAllBookings() {
    return bookingRepository.findAll();
  }
}

Explanation

Create a REST controller to expose APIs for creating and retrieving bookings.

Code:

@PostMapping
  public Booking createBooking(
@RequestBody Booking booking) {
    return bookingService.saveBooking(
booking);
  }

  @GetMapping
  public List<Booking> getBookings() {
    return bookingService.getAllBookings();
  }

Explanation

Configure application.properties to connect Spring Boot with a database such as MySQL.

Code:

spring.datasource.url=jdbc:mysql://
localhost:3306/booking_db
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Explanation

Test the booking API using POST and GET requests from tools like Postman or a React frontend booking form.

Code:

// POST /bookings
{
  "customerName": "hackforge",
  "bookingDate": "2026-04-25"
}

// GET /bookings

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community