'How to skip null field with lombok @ToString
I found with no succes the simple way to use lombok @toString with the skip null field behaviour.
I think create my own toString function for all function using the aspect programmation. Like that i can chek all null field and skip that.
But it is the good practice, or lombok @toString has one option to do that simply?
Best Regards
Solution 1:[1]
It's an open issue on Lombok, so it's not part of implementation yet. see #1297.
Solution 2:[2]
I could not find a Lombok way of doing it, so I postprocess the generated String using the following method:
/**
* Removes the null values from String generated through the @ToString annotation.
* For example:
* - replaces: AddressEntity(id=null, adrType=null, adrStreet=null, adrStreetNum=null, adrComplement=null, adrPoBox=null, adrNip=null, adrCity=city, adrCountry=null, adrNameCorresp=nameCorresp, adrSexCorresp=null, adrSource=null, adrSelectionReason=null, validityBegin=null, validityEnd=null, lastModification=null, dataQuality=null)
* - by: AddressEntity(adrCity=city, adrNameCorresp=nameCorresp)
* Note: does not support tricky attribute content such as "when, x=null, it fails".
* @param lombokToString a String generated by Lombok's @ToString method
* @return a string without null values
*/
public static String removeToStringNullValues(String lombokToString) {
//Pattern
return lombokToString != null ? lombokToString
.replaceAll("(?<=(, |\\())[^\\s(]+?=null(?:, )?", "")
.replaceFirst(", \\)$", ")") : null;
}
Note that tricky object attributes such as "when, x=null, it fails" are not supported (but that's not an issue for my use-case). I could have used https://commons.apache.org/proper/commons-lang/javadocs/api-3.9/org/apache/commons/lang3/builder/ReflectionToStringBuilder.html to generate the "toString" content, but I wanted to reuse the field exclusion logic behind Lombok @ToString(excludes="myExcludedField").
Solution 3:[3]
You can override toString method like below
public class MyClass{
field a;
field b;
@Override
public String toString() {
Field[] fields = MyClass.class.getDeclaredFields();
String res = "";
for(int x = 0; x < fields.length; x++){
try {
res += ( fields[x].get(this)) != null ? fields[x].getName() + "="+ (fields[x].get(this).toString()) + "," : "";
} catch (Exception ex) {
}
}
return res;
}
Solution 4:[4]
Not Lombok-y but this helped me, while waiting patiently for Lombok to include this feature in...
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
@Override
public String toString() {
ReflectionToStringBuilder rtsb = new ReflectionToStringBuilder(this);
rtsb.setExcludeNullValues(true);
return rtsb.toString();
}
Solution 5:[5]
With Guava:
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.omitNullValues()
.add("fieldA", fieldA)
.add("fieldB", fieldB)
.toString();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pushkar Adhikari |
| Solution 2 | Julien Kronegg |
| Solution 3 | Udisuarez |
| Solution 4 | bratan |
| Solution 5 | Sergii Poltorak |
