Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 515
Artegon Avatar asked Jan 18 '17 11:01

Artegon


People also ask

What is JSR 303 in Spring MVC?

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.

What is the JSR 303 Bean Validation?

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.

How to implement jsr-303 validation in Java?

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.

How to use jsr-303 annotations with Spring Boot?

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


Video Answer


1 Answers

This can now be done by using PropertyNodeNameProvider.

like image 198
Damir Alibegovic Avatar answered Sep 29 '22 14:09

Damir Alibegovic