Spring validate string value is a JSON

女生的网名这么多〃 提交于 2020-01-24 09:39:06

问题


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.


回答1:


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 = ContactNumberValidator.class)
@Target( { ElementType.METHOD, 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.



来源:https://stackoverflow.com/questions/59203967/spring-validate-string-value-is-a-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!