In my spring boot rest application I'm doing validation via annotation and has the following class, how do I set a custom field name for the fields and use them in the error messages, the below example works, I can retrieve the value from the JSON payload and include them in the error message.
I need to set the name of the field to 'E-mail address' so I can put it in the error message.
The application also does not have any forms, input is received from json.
public class Contact {
@Size(max = 20, message = ErrorMessages.INVALID_LENGTH)
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public class ErrorMessages {
public static final String INVALID_LENGTH = "'${validatedValue}' must be between {min} and {max} characters long";
}
EDIT:
JSON input
{
"email": "[email protected]"
}
expected output:
{
"timestamp": "2022-06-13T00:32:28.861 00:00",
"status": 400,
"errors": [
"E-mail address must be between 0 and 20 characters long"
]
}
I don't want to hard code the field name in the error message since I want to reuse it on other fields for checking their length. I want to do something similar to ${validatedValue} but with the name instead.
UPDATE:
Ok seems that setting a 'label' on a field to use in interpolation is not possible, I just created a class that contains the display names that I want to use and appended the error message to it:
public class FieldName {
public static final String E_MAIL_ADDRESS = "E-mail address";
}
public class ErrorMessages {
private static final String INVALID_LENGTH = " '${validatedValue}' must be between {min} and {max} characters long";
public static final String E_MAIL_INVALID_LENGTH = FieldName.E_MAIL_ADDRESS INVALID_LENGTH;
}
The payload class
public class Contact {
@Size(max = 20, message = ErrorMessages.E_MAIL_INVALID_LENGTH)
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Fixed output:
{
"timestamp": "2022-06-13T03:27:03.207 00:00",
"status": 400,
"errors": [
"E-mail address '[email protected]' must be between 0 and 20 characters long"
]
}
CodePudding user response:
UPDATE: Ok seems that setting a 'label' on a field to use in interpolation is not possible, I just created a class that contains the display names that I want to use and appended the error message to it:
public class FieldName {
public static final String E_MAIL_ADDRESS = "E-mail address";
}
public class ErrorMessages {
private static final String INVALID_LENGTH = " '${validatedValue}' must be between {min} and {max} characters long";
public static final String E_MAIL_INVALID_LENGTH = FieldName.E_MAIL_ADDRESS INVALID_LENGTH;
}
The payload class
public class Contact {
@Size(max = 20, message = ErrorMessages.E_MAIL_INVALID_LENGTH)
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Fixed output:
{
"timestamp": "2022-06-13T03:27:03.207 00:00",
"status": 400,
"errors": [
"E-mail address '[email protected]' must be between 0 and 20 characters long"
]
}