How to globally configure spring-ws / spring-boot-starter-web-services to serialize also empty (null) elements without modifying the serialized class?

Viewed 116

How to globally configure spring-ws / spring-boot-starter-web-services to serialize also empty (null) elements?

I cannot modify the serialized classes themselves, so I cannot add nillable = true such as

public class Response {
    @XmlElement(name = "FirstName", required=true, nillable = true)
    private String firstName;
}

I want to have all the empty elements present (in any form, but present) in the response even if they are null, such as

<Response>
    <FirstName whatever-attribute-here/> <!-- anything here, just the element should be present here even if it is not set in the object -->
</Response>

Once again, I cannot modify the Response class and it does not contain the nillable = true attributes.

My motivation is that I am to implement the response mapping and I want to have a quick check (during the development) that I have not omitted any of the tens of elements. Setting all omitted elements manually to an empty string (or rather something more reasonable) is already what I want to achieve, it's the goal and not the way :)

I was trying to search the answer but all the results take me to the modification of the XML class itself or to setting the elements to an empty string. I believe there might be a nice global switch :)

In JSON, there is an analogical global switch: I can set globally (https://www.baeldung.com/jackson-ignore-null-fields) either

mapper.setSerializationInclusion(Include.ALWAYS); // properties are always included, independent of value of the property

or

mapper.setSerializationInclusion(Include.NON_NULL); // only properties with non-null values are to be included
0 Answers
Related