0

I have a string field that may contain whitespaces at leading and trailing. I want to trim these whitespaces and return trimmed text using ConstraintValidator. If the text is null, I want to return null.

When looking at the implementation examples as shown on this link, I am not sure how can I create a method that gets string and return string instead of isValid() method. So, how can I implement this approach based on the given scenario?

  • Maybe you can explain why you want to do this, so we can come up with better solution. What you want to do is possible with validator annotation on the class level, but that goes against the single responsibility principle. A `ConstraintValidator` should only validate, it should **not** validate and mutate. – Chaosfire Jun 08 '22 at 16:08
  • @Chaosfire I agree with you, but the reason is that: I want to trim whitespaces and I do not want to trim the text by using an Util method or in getter of the property. Instead, I want to use annotation on each property that will be trimmed. So, how can I use a similar way to custom validation annotation and trim the related properties by using a custom annotation in Spring Boot or Java? –  Jun 08 '22 at 16:13
  • @Chaosfire Any reply please? –  Jun 08 '22 at 16:30
  • Sounds like a pojo, since you only ever need trimmed strings, why not just trim them in the setters? Any reason not to do it? – Chaosfire Jun 08 '22 at 16:42
  • @Chaosfire Yeah, I tried to set in setter and using an Util method. However, I am looking a solution by using annotation and applying the rule easily to all of the necessary properties as in validators. So, is there any approach that can be used to modify property value (e.g. returning trim value or null if the value is null) **via annotation**? –  Jun 09 '22 at 07:34
  • Sounds like something that could be done with aspects, although i am not too sure. Check this [question](https://stackoverflow.com/questions/12372165/intercepting-method-calls). – Chaosfire Jun 09 '22 at 09:56
  • JsonDeserialize seems to be ok according to my researches after the answer of @ankit. Thanks a lot for your helps. –  Jun 09 '22 at 11:10

1 Answers1

0

Use jackson JsonDeserialize annotation to do this.

Example:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class TrimWhiteSpace extends StdDeserializer<String> {

  private static final long serialVersionUID = -4993230470571124275L;

  public TrimWhiteSpace() {
    this(null);
  }

  protected TrimWhiteSpace(final Class<?> vc) {
    super(vc);
  }

  @Override
  public String deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
    return p.getText() == null ? null : p.getText().trim();
  }
}

You can use this annotation as below:

public class Request {

    @JsonDeserialize(using = TrimWhiteSpace.class)
    private String name;

}
ankit
  • 94
  • 5
  • You rock!.. Voted+. By the way, could you please clarify me about the following issue? –  Jun 09 '22 at 08:54
  • **1.** What about using `JsonDeserializer` instead of `StdDeserializer`? What is the difference and which one is better for this scenario? –  Jun 09 '22 at 08:55
  • **2.** Do we really need to use `serialVersionUID` for this example / scenario? –  Jun 09 '22 at 08:55
  • **3.** What is the purpose of 2 constructors? Could you please explain a little bit? –  Jun 09 '22 at 08:57
  • 1
    StdDeserializer contains some base functionalities such as parsing from string. You can use these methods in your custom deserializer. – ankit Jun 09 '22 at 17:53
  • 1
    Javadoc of JsonDeserializer class recommends this: Custom deserializers should usually not directly extend this class, but instead extend com.fasterxml.jackson.databind.deser.std.StdDeserializer (or its subtypes like com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer). – ankit Jun 09 '22 at 17:58
  • 1
    2. No need for a serialVersionUID in this scenario. – ankit Jun 09 '22 at 18:02