JSR-303 validation in Spring controller and getting @JsonProperty name

我怕爱的太早我们不能终老 提交于 2020-05-15 02:44:58

问题


I do validation with JSR-303 in my Spring app, it works as needed.

This is an example:

@Column(nullable = false, name = "name")
    @JsonProperty("customer_name")
    @NotEmpty
    @Size(min = 3, max = 32)
    private String name;

And REST API clients use customer_name as name of input field that send to API bud validation field error org.springframework.validation.FieldError returns name as name of the field.

Is there some way hot to get JSON-ish name that is specified in @JsonProperty? Or do I have to implement own mapper to map class fields name into its JSON alternative?

Edit1: Renaming class fields into names that correspond to JSON names is not alternative (for many reasons).


回答1:


This can now be done by using PropertyNodeNameProvider.




回答2:


There is no way to achieve this currently. We have an issue for this in the reference implementation: HV-823.

This would address the issue on the side of Hibernate Validator (i.e. return the name you expect from Path.Node#getName()), it'd require some more checking whether Spring actually picks up the name from there.

Maybe you'd be interested in helping out with implemeting this one?



来源:https://stackoverflow.com/questions/41717866/jsr-303-validation-in-spring-controller-and-getting-jsonproperty-name

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