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).
JSR 303 specification allows the validation rules to be specified directly into the fields inside any Java class which they are intended to validate, instead of creating validation rules in separate classes. So far, we have learned about bean validations in Spring MVC using BindingResult.rejectValue () and custom validator implementation.
JSR-303 bean validation has become the validation standard in the Java world. Bean validation API provides an object level constraint declaration and validation facility for the Java application developer, and a constraint metadata repository and query API.
For validation to actually work, you need an implementation as well, such as Hibernate Validator. 2. Applying JSR-303 Annotations After adding JSR-303 dependencies, the first thing you need to do is decorate a Java bean with the necessary JSR-303 annotations.
To use JSR-303 annotations with Spring, you will need to add below dependency in pom.xml. For validation to actually work, you need an implementation as well, such as Hibernate Validator. 2. Applying JSR-303 Annotations
This can now be done by using PropertyNodeNameProvider.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With