If I have the following dto in spring boot
public class PersonDto {
@JsonProperty(access = Access.READ_ONLY)
private Long id;
@NotBlank
private String name;
//@NotNull
@JsonProperty(access = Access.READ_ONLY)
private Integer age;
}
And I want to perform a HTTP PATCH following this standard, which is implemented by the JsonPatch library, I would write something like this to get the patched JsonNode
JsonNode patched = patch.apply(objectMapper.convertValue(target, JsonNode.class));
However when performing a patch operation like the following, the @JsonProperty(access = Access.READ_ONLY)
won't make any effect(meaning that age would be set to 12)
[ {"op":"replace","path":"/age","value": 12} ]
I've also tried the following afterwards
return objectMapper.treeToValue(patched, PersonDto.class)
But here the age is set to null
So, the expected behaviour I want to achieve is the following:
Whenever I receive a patch operation that attempts to replace the value of 'age', I want that operation to be ignored, since age is read-only.
I know I could create if statements in the controller to check each patch operation and ignore it if it attempts to change a field that I want to be read-only like age, but I would rather do this with jackson annotations. Is this possible?
I did follow this tutorial in case you want to check it out