I have a Spring Boot service that uses a custom POJO class for a POST request body. When client passes one of the fields as null, I would like that value to be set to 0.
@Data
public class CardAnswer {
private Integer answerId = 0; //should always be 0 and never set to null
private String value;
}
Used by the following controller:
@RequestMapping(value = "/{category}", method = RequestMethod.POST)
public SalesPulseMResponse updateSecurity(@PathVariable(value = SalesedgeConstants.CATEGORY)
@RequestBody CardAnswer requestBody) {
return fooService.updateFieldsByCategory(category, requestBody);
}
Spring boot will handle the data binding from JSON to the CardAnswer for me, but whenever answerId is passed as null, I want it to retain 0. Is there a proper way to do that or would I have to parse through the class and set the values myself? Currently, if the client doesn't pass in this field in their request JSON, it will retain the default value, however, I want that default value to be retained even for null values that were passed in.