How do you implement Redis caching in Spring Boot?
Add the Redis and cache starters, enable caching and use @Cacheable, @CachePut and @CacheEvict with a TTL and JSON serializer for shared, fast caching.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Add the Redis and cache starters, enable caching and use @Cacheable, @CachePut and @CacheEvict with a TTL and JSON serializer for shared, fast caching.
@ResponseStatus ties an HTTP status code to an exception class or handler method in Spring. See examples, limits and when to use ResponseEntity instead.
Use @RestControllerAdvice and @ExceptionHandler to return consistent JSON errors. Covers validation failures and the RFC 9457 ProblemDetail type in Spring 6.
@ControllerAdvice applies shared exception handling, binding and model logic to all Spring controllers. See examples with @RestControllerAdvice and ProblemDetail.
Learn how to implement custom error handling in Spring Boot with @RestControllerAdvice, ProblemDetail responses and validation error details.
readOnly = true marks a transaction as read-only, letting JDBC, Hibernate and routing layers optimise queries without guaranteeing that writes fail.
Learn which exceptions JPA throws in Spring Boot, why try/catch around save often fails, and how to map errors to 409 and 404 responses.
@Valid runs Jakarta Bean Validation on controller arguments and nested objects in Spring. See DTO examples, error handling and how it differs from @Validated.
Create custom Bean Validation constraints for JPA entities, validate on persist and update, and know where database constraints still matter.
@NotNull rejects null values in Jakarta Bean Validation. See Spring Boot and JPA examples and how it differs from @NotEmpty and @NotBlank.
Annotate JPA entities with Jakarta Bean Validation constraints, learn when Hibernate validates them, and combine them with database constraints.
@EnableJpaRepositories registers Spring Data JPA repositories. See when Boot auto-configures it and how to scan custom packages or data sources.
Set the datasource under spring.datasource, JPA behaviour under spring.jpa and Hibernate settings under spring.jpa.properties. Includes safe production defaults.
The spring-boot-starter-data-jpa dependency bundles Spring Data JPA, Hibernate and transactions, and auto-configures them from your database properties.
Step-by-step Spring Boot 3 guide to JPA: dependencies, datasource settings, entities, repositories, services, REST endpoints, and testing.
Spring Data repository methods are transactional by default. Learn when to override with @Transactional for modifying queries, propagation and timeouts.
Add custom methods to Spring Data repositories using fragment interfaces and Impl classes, or a shared base repository class for every repository.
The EntityManager is the core JPA interface: it manages entities in a persistence context and runs persist, find, merge, remove and queries within transactions.
Learn how @Modifying and @Query run bulk UPDATE and DELETE statements in Spring Data JPA, and how to avoid stale entities and missing transactions.
Learn how to create and use PageRequest in Spring Data: the factory methods, sorting, navigation and the SQL it produces.