Given a POJO in Spring Boot with several dozen fields of type String
which is deserialized by Jackson. For demonstration purposes the following example only contains three fields:
@NoArgsConstructor
public class SomeRequest {
@JsonProperty("field_1")
private String field1;
@JsonProperty("field_2")
private String field2;
@JsonProperty("field_3")
private String field3;
}
I'm looking for a way to override the setter method but only for certain fields, i.e. I'd like to avoid repeating the below code for every affected field. This is doable for a handful number of fields but gets tedious for more than a handful.
public setField2(String field2) {
this.field2 = field2 + "?";
}
My idea was to place an annotation on the field like this:
@NoArgsConstructor
public class SomeRequest {
// ...
@JsonProperty("field_2")
@AppendQuestionMark
private String field2;
// ...
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AppendQuestionMark {
}
But I'm lacking information on how to "implement" the AppendQuestionMark
annotation which would override the field's setter method.
Or am I thinking way too complicated?