I have some value from my configuration file, that should be a JSON (which will be loaded as a String).
I'd like Spring to validate that this value is indeed a valid JSON before injecting it and throw an error else.
I've read about the validation annotations that exist, such as - @NotNull, @Size, @Min, @Max, @Email, @NotEmpty etc.
Is there any way to create a custom validator?
I want a validator that will attempt to convert the String to JSON, as in the following example: How to convert jsonString to JSONObject in Java.
This is not provided by available validation annotations, therefore you have to go for a custom implementation. The task is divided into 2 simple steps:
1. Is a given String is in the JSON format
There are multiple libraries that are able to parse (therefore validate) a String whether follows the JSON syntax standard. Let's use my favourite GSON for example (there are many). It depends on what library do you currently use:
String string = "{\"foo\":\"bar\"}"
JsonParser jsonParser = new JsonParser();
try {
    jsonParser.parse(string);       // valid JSON
} catch (JsonSyntaxException ex) { 
    /* exception handling */        // invalid JSON
}
2. Custom validation annotation
Start with providing a dependency enabling validations:
org.hibernate
hibernate-validator
Create an annotation used for the validation:
@Documented
@Constraint(validatedBy = JsonStringValidator.class)
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonString {
    String message() default "The String is not in JSON format";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
... and the validator handling the validation through the annotation:
public class JsonStringValidator implements ConstraintValidator<JsonString, String> {
    @Override
    public void initialize(JsonString jsonString) { }
    @Override
    public boolean isValid(String string, ConstraintValidatorContext context) {
        // Use an implementation from step 1. A brief example:
        try {
            new JsonParser().parse(string);
            return true;                    // valid JSON, return true
        } catch (JsonSyntaxException ex) { 
            /* exception handling if needed */
        }
        return false;                       // invalid JSON, return false
    }
}
The usage is pretty straightforward:
@JsonString
private String expectedJsonString
This implementation is described in detail at Baeldung's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With