I am trying to implement a static validation that prevents a value from being passed into a JSON payload to POST and PUT endpoints.
For example, I have this DTO:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SampleApiDto {
private long id;
private String description;
private String columnName;
private String code;
}
id
is a generated value on our data service side. Right now, if you provide a payload like this:
{
"id": 123,
"description": "test10",
"columnName": "test",
"code": "1"
}
it throws a detached entity error and returns 500. Ideally, I would like it to return a 400 error with a static validation that id
is present in the payload. Due to the size of our codebase, we do not want to implement if statements in the controller. We would rather this be an annotation, even if it were a custom annotation.
I have tried using @Null
and @JsonIgnore
on the getter method, and these did not work both ways. The ID should be accessible afterwards in the DTO object from the data service.
Thanks so much!