Category: Spring Boot
-
Custom error message when validation fails
To make your Spring Boot REST API return custom error messages when validation fails, you need to: Step-by-Step Guide Step 1: Add Validation Dependency If you’re using Maven, make sure you have this in your pom.xml: Or for older versions: Step 2: Annotate Your Request DTO Example DTO with validation annotations: Step 3: Use @Valid…
-
HTTP Response Status Codes
1. Introduction Every time a client (browser, mobile app, API consumer) sends a request to a server, the server responds with an HTTP status code that informs the client of the outcome. These codes are three-digit numbers grouped into 5 major categories. 2. 1xx: Informational Responses These codes indicate that the request has been received…
-
Spring @Transactional Isolation Levels
In Spring, the @Transactional annotation is used to define transactional behavior for a method or class. One important aspect of transactions is isolation level, which determines how one transaction is isolated from others. Isolation levels help prevent data anomalies like dirty reads, non-repeatable reads, and phantom reads. 1. Understanding Isolation Levels A transaction isolation level…
-
Understanding Transaction Propagation in Spring
1. What Is Transaction Propagation? In Spring, when a method is annotated with @Transactional, Spring manages the transaction automatically. But what happens when a method annotated with @Transactional calls another transactional method? Propagation defines how transactional behavior should behave in nested method calls — whether to continue an existing transaction, start a new one, or…
-
Customizing the ConcurrentMapCacheManager
1. Introduction By default, Spring Boot provides a simple in-memory cache via ConcurrentMapCacheManager.This is ideal for testing, prototyping, or small-scale applications where we don’t want to set up an external cache provider. However, we can customize its behavior to control things like: 2. Project Setup We’ll customize caching by creating a configuration class in the…
-
Spring Boot Caching with Annotations – A Comprehensive Guide
1. Caching Fundamentals Caching is essentially storing frequently used data in a temporary storage location (cache) close to your application so that it can be accessed much faster compared to fetching it from a slower source such as: 1.1 Why Caching Matters 1.2 Key Caching Operations in Any Application When implementing caching in Spring Boot,…
-
Using @JsonComponent in Spring Boot
What Problem Does @JsonComponent Solve? Before @JsonComponent (Manual Approach) Before Spring Boot introduced @JsonComponent, if you wanted to register a custom serializer or deserializer with Jackson, you had to manually register it with the ObjectMapper. For example: This was: After @JsonComponent (Automatic Approach) Spring Boot introduced @JsonComponent to simplify this. Now, you can: No manual…
-
Java Records for @ConfigurationProperties
Java record classes can be used for @ConfigurationProperties to create immutable, type-safe configuration objects with minimal boilerplate code. You define a record with fields that correspond to your configuration properties, and annotate it with @ConfigurationProperties and a prefix. Spring Boot automatically binds properties from your application.properties or application.yml file to the record’s fields. You can…
-
Spring @HttpExchange: The New and Improved Way to Build HTTP Clients
A new and powerful feature introduced in Spring Framework 6 and Spring Boot 3 is the declarative HTTP client, enabled by the @HttpExchange annotation. This modern approach simplifies the creation of HTTP clients by allowing you to define them as Java interfaces, completely removing the need for boilerplate code that was common with older clients…
-
JpaRepository vs CrudRepository
In Spring Data JPA, both CrudRepository and JpaRepository are interfaces used to access and manipulate data in the database. However, there are key differences in terms of functionality, inheritance hierarchy, and use cases. Below is a detailed comparison: 1. Inheritance Hierarchy 2. Package and Purpose Feature CrudRepository JpaRepository Package org.springframework.data.repository org.springframework.data.jpa.repository Purpose Basic CRUD operations…
