问题
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