Set POJO field value based on the value of another field in Spring Boot

  Kiến thức lập trình

I have a simple POJO class

class POJO{
private boolean isEnabled,
private String field
}

I have a controller that accepts RequestBody of type POJO, as part of a change I want to include a validation such that when isEnabled is true in the RequestBody set the value of field to “TEST” (value of field in RequestBody will be null) and when isEnabled is false set the value of field to value received from RequestBody

I can implement a if…else… on the controller/service but this involves a lot of code replication especially since i have multiple fields to set, Is there a way i can incorporate this validation within the POJO class itself?

LEAVE A COMMENT