简体   繁体   中英

Spring validate string value is a JSON

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:

  • groupId: org.hibernate
  • artifactId: 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM