JsonProperty值作为ConstraintViolati

JsonProperty值作为ConstraintViolati

本文介绍了Hibernate Validator和Jackson:使用@JsonProperty值作为ConstraintViolation PropertyPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个简单的POJO,如下面注释了Jackson 2.1和Hibernate Validator 4.3.1注释:

 最终的公共课程Person {
@JsonProperty(nm)
@NotNull
final public String name;

public Person(String name){
this.name = name;
}
}

我将这样的JSON发送到网络服务:

  {name:null} 

Hibernate报告ConstraintViolation时使用类成员标识符name而不是JsonProperty注释值。有谁知道是否有可能让Hibernate Validator查看类的注释并使用该值代替?

解决方案

不,那是不可能的。 Hibernate Validator 5(Bean Validation 1.1)具有 ParameterNameProvider 的概念,它返回了在违反方法参数约束的情况下报告的名称,但没有任何可比较的属性约束。 / p>

Say I have a simple POJO like below annotated with Jackson 2.1 and Hibernate Validator 4.3.1 annotations:

final public class Person {
  @JsonProperty("nm")
  @NotNull
  final public String name;

  public Person(String name) {
      this.name = name;
  }
}

And I send JSON like such to a web service:

{"name": null}

Hibernate when it reports the ConstraintViolation uses the class member identifier "name" instead of the JsonProperty annotation value. Does anyone know if it is possible to make the Hibernate Validator look at the annotation of the class and use that value instead?

解决方案

No, that's not possible. Hibernate Validator 5 (Bean Validation 1.1) has the notion of ParameterNameProviders which return the names to reported in case method parameter constraints are violated but there is nothing comparable for property constraints.

这篇关于Hibernate Validator和Jackson:使用@JsonProperty值作为ConstraintViolation PropertyPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:07