When sending JSON data with camelCase properties to a Spring controller, the @RequestBody annotated object may fail to properly map the incoming data if the property naming conventions don't match.
// Example problematic request object
public class InspectionRequest {
private String inspectionId;
private String recordDate;
private String formType;
}
Solution Approaches
Option 1: Modify Client-Side Property Naming
Change the JSON property names to snake_case format to match Spring's default property naming strategy:
{
"inspection_id": "123",
"record_date": "2023-01-01",
"form_type": "DAILY"
}
Option 2: Use @JsonProperty Annotation (Recommended)
Annotate your Java class properties with @JsonProperty to explicitly define the JSON field names:
import com.fasterxml.jackson.annotation.JsonProperty;
public class InspectionRequest {
@JsonProperty("inspectionId")
private String inspectionId;
@JsonProperty("recordDate")
private String recordDate;
@JsonProperty("formType")
private String formType;
}
The second approach is generally preferred as it:
- Maintains consistent naming conventions across the codebase
- Provides epxlicit mapping documentation
- Doesn't require client-side changes