自定义 Json 在 anypoint studio 中验证

Custom Json Validation in anypoint studio

我正在尝试使用 json 架构验证器来验证 JSon。但它 return 是一条通用消息。 “Json 内容不符合模式”。 我有一个 HTTP POST REQUEST 发送有效载荷如下:

{ "key1" : "value1", "key2" : "value2" ,"key3": "value3" }

如果 key1 和 key2 丢失。我希望它给出如下错误信息:

{{
  errorCode :1001,
  errorMessage : key1 is missing",
},
  errorCode :1002,
  errorMessage : key2 is missing"
}
}

我尝试将错误写入文件(json 包含所有警告和消息的文件} 看起来像这样:

{
  "level" : "error",
  "domain" : "validation",
  "keyword" : "required",
  "message" : "object has missing required properties ([\"key1\",\"key2\",\"key3\"])",
  "required" : [ "key1", "key2", "key3"],
  "missing" : [ "key1", "key2"]
}

这只是此文件的一小部分。我必须遍历文件才能获得此信息。有没有其他方法,我可以执行自定义验证并 return 向用户发送正确的错误消息。

编辑 1:

我创建了以下 RequestObj class:

public class RequestObj {

@Valid
@NotBlank
@NotNull
private String key1;

@Valid
@NotBlank
private String key2;

@Valid
@NotBlank
private String key3;

@Override
public String toString() {
    return "RequestObj [key1=" + key1 + ", key2=" + key2 + ", key3=" + key3 + "]";
}

它没有验证 key1 是否为空。

邮递员请求:

POST/验证HTTP/1.1 主持人:localhost:8081 内容类型:application/json

{ "key2" :"gg", "key3" : "hh" }

编辑 2:

当我实现验证器接口时。我无法访问 mule 事件。在这种情况下,我将如何访问我需要验证的 json?

如果内置 JSON 验证器没有提供您需要的验证或错误报告,那么您必须在 Java 中实现您自己的自定义验证器。请参阅有关如何实现 custom validator by extending the Validator interface and in your implementation class you can use any Java library to validate JSON, like for example Jackson or GSON. Then you can customize the error handling.

的文档

这是对 json 输入执行自定义验证后我的结果。 我使用 JSR-303 注释来验证数据。

class Example{ 
   @NotBlank
        @Size(min = 3, max = 5)
        private String key1;

        @Pattern(regexp=".+@.+\.[a-z]+") // email
        private String key2;

        private String key3;
}

然后我编写了一个自定义验证器,并通过传递所有值来调用静态函数验证:

public class ValidationServiceImpl {

    public static HashMap<String,String> validate(String key1 , String key2 , String key3)  {
        HashMap<String,String> result = new HashMap();
        Example req = new Example(key1 , key2, key3);
    Validator validator;
    ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
    validator = validatorFactory.getValidator();
    Set<ConstraintViolation<Example>> violations = validator.validate(req);
    if(!CollectionUtils.isEmpty(violations)) {
        for (ConstraintViolation<Example> violation : violations) 
        {
            String propertyPath = violation.getPropertyPath().toString();
            String message = violation.getMessage();
            result.put(propertyPath, message);
        }
    }
    return result;
    }

}

结果是 returns 所有 violations.Logging 的哈希图,它会给你结果。

所需的 POM 依赖项是:

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.1.0.Final</version>
</dependency>
   <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator-annotation-processor -->
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator-annotation-processor</artifactId>
    <version>6.1.0.Final</version>
</dependency>


    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish/javax.el -->
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.el</artifactId>
    <version>3.0.0</version>
</dependency>