0

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: "TEST@TEST.COM" Then it would automatically be formatted to: "test@test.com"

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?

WickedElephant
  • 327
  • 2
  • 9
  • Are you using _Spring Boot_ or _Spring MVC_? Should your _conversions_ only apply from request Strings to DTO Strings, i.e. cleaning during request deserialization? What is user-input: HTTP __request-parameters__ or request-body (e.g. JSON/XML) too? – hc_dev Feb 26 '21 at 06:48

2 Answers2

1

If you are using Spring Mvc, you have access to a formatting API. There are pre-existing annotations in the org.springframework.format.annotation package for date and number formatting.

If you need custom formatting rules, you can write your own and register it via the FormattingConversionServiceFactoryBean.

See some examples here: https://docs.spring.io/spring-framework/docs/3.2.0.RC1/reference/html/validation.html

Luciano Fiandesio
  • 10,037
  • 10
  • 48
  • 56
  • Thank you. This isn't exactly what I was after, but it doesn't look like what I am looking for exists. The Spring solution is pretty heavy weight, and not as easy as just defining annotation based type converters at the field level, which is more what I was looking for. – WickedElephant May 20 '20 at 00:27
1

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 TEST@EXAMPLE.COM. This should the be converted to lowercase and strored in the DTO's property (a.k.a attribute or field ) email as value test@example.com.

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

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • You are correct, in that I am ultimately referring to "type conversion". Guess I was using the wrong terminology. Thank you for your answer. It seems creating custom Jackson deserializers might be the way to go, although I have already moved past this, and ended up just formatting the input via the setters. – WickedElephant Mar 23 '21 at 20:42