Issue
This seems like it would be a really common thing to need in just about any Java project. And yet I am unable to find anything that serves this purpose.
I used to use a web framework called Stripes Framework, that had annotation driven validators and automatic formatting via their validator API.
Example:
@Validate(maxlength = ModelConstants.NAME_MAX_LENGTH, converter = CapitializeFullyTypeConverter.class)
private String name;
@Validate(maxlength = ModelConstants.NAME_MAX_LENGTH, converter = EmailTypeConverter.class)
private String email;
Those fields would be defined on your controller action bean, and all user input would automatically be validated and formatted according to the specified rules.
If someone would enter their email address as: "[email protected]" Then it would automatically be formatted to: "[email protected]"
I would like to find something like this for automatically formatting data within DTOs.
If nothing like this is available, what is the usual way of handling this? Surely everyone is not writing custom formatting functions for each getter within the DTO?
Solution
Assumption: meaning of "formatter" as type-converter
When you ask for annotation based "formatter" you mean Stripe Framework's @Validate
annotation with its parameter converter
. The converter can be parameterized as:
(class) The converter class that will be used to transform this parameter into its object representation
In the Web-MVC context this means the converter is used to convert the request-parameter, exactly its source value, as supplied by the incomming web-request. The converted value is then stored as target value to the (DTO) object's property.
Example: Conversion of (DTO) Strings
Web request is given incommong with parameter name email and value string [email protected]
. This should the be converted to lowercase and strored in the DTO's property (a.k.a attribute or field ) email as value [email protected]
.
Spring annotations for DTOs
Spring Framework also uses the concepts of request validation and request conversion. It also allows to configure these annotations-driven on the DTOs or classes and their attributes.
The validation leverages Java Bean Validation standard (JSR-303) javax.validation
annotations like @Valid
and @Length
.
The conversion is part of the HttpConverters used for serialization/deserialization of responses/requests to/from objects (DTO). Therefore Spring allows to inlude object-relational mapper (ORM) frameworks or libraries as (e.g. Maven) dependencies. These bring their own annotations regarding conversation with them.
Fasterxml's Jackson
Jackson (ORM) for example uses the annotation @JsonFormat
to achieve simple string conversions - but only for non-string fields like Date or Number (as far as I know).
It also allows complete customisation of serializers and deserializers via annotations @JsonSerialize
and @JsonDeserialize
.
Note, that although many annotations in Jackson are named with "Json" they mostly also apply to XML or even other data formats (representations) like CSV, etc.
String To Lowercase with Jackson
Your question was asked for Jackson: How to force Jackson deserialize field values to lower case and answered.
See also
- Difference between @Valid and @Validated in Spring
- Use Jackson to deserialize JSON string or object into a String field
- DZone: Tutorial on Jackson Deserialization Annotations
- Bealdung (2019): Getting Started with Custom Deserialization in Jackson
Answered By - hc_dev