To implement RESTful services with Spring Boot, add spring-boot-starter-web and spring-boot-starter-data-jpa, define a JPA entity and a JpaRepository, then expose it through a @RestController that maps GET, POST, PUT and DELETE requests. Spring Boot auto-configures the embedded server, JSON conversion and the datasource, so the whole stack runs from a single main method.
This guide builds a complete books API with an H2 database, a service layer, validation and tests, using current Spring Boot conventions (Java 21 or 25 LTS, jakarta.* packages).
Generate a project at start.spring.io with these dependencies: Spring Web, Spring Data JPA, Validation and H2 Database. The key Maven entries are:
Add a minimal application.properties:
ddl-auto=update is fine for a demo. In production, manage the schema with Flyway or Liquibase instead. Setting open-in-view to false avoids keeping a database session open during view rendering.
The entity maps to a table. Hibernate needs a no-argument constructor and mutable fields, so a regular class is a better fit here than a record.
Spring Data generates the implementation at startup, including save, findAll, findById and deleteById. The @Repository annotation is not required on the interface.
Inject the repository through the constructor. A single constructor needs no @Autowired.
Exposing entities directly is acceptable for a small service. For larger APIs, use separate request and response records so you do not leak internal fields or let clients set the id.
Start the application and try each operation with curl:
As logic grows, move it out of the controller into a @Service class with @Transactional methods, and throw a domain exception for missing records. Map that exception once in a @RestControllerAdvice.
ProblemDetail produces an RFC 9457 application/problem+json body, which clients can parse in a standard way.
@SpringBootTest with MockMvc exercises the controller, JSON mapping and repository together.
No. JPA is only one way to store data. A REST controller works with any data source, including JDBC, MongoDB or an in-memory map.
Return a DTO (a record works well) once the API is public, so database changes do not break clients and sensitive fields stay private.
Accept a Pageable parameter in the controller and call repository.findAll(pageable). Spring resolves page, size and sort request parameters automatically.
Port 8080 by default. Change it with server.port in application.properties.
A Spring Boot REST service needs four parts: an entity, a Spring Data repository, a @RestController with HTTP method mappings, and consistent error handling. Return proper status codes, validate input with @Valid, keep the schema under migration control in production, and test the API with MockMvc.