微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不修改序列化类的情况下全局配置 spring-ws / spring-boot-starter-web-services 以序列化空空元素?

如何解决如何在不修改序列化类的情况下全局配置 spring-ws / spring-boot-starter-web-services 以序列化空空元素?

如何全局配置 spring-ws / spring-boot-starter-web-services 以序列化空 (null) 元素?

我不能自己修改序列化的类,所以我不能添加nillable = true

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

我希望在响应中所有空元素呈现(以任何形式,但存在),即使它们是null,例如

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

再一次,我无法修改 Response 类并且它包含 nillable = true 属性

我的动机是我要实现响应映射,我想快速检查一下(在开发过程中)我没有遗漏数十个元素中的任何一个。手动将所有省略的元素设置为空字符串(或者更合理的东西)已经是我想要实现的,这是 目标 而不是 way :)

我试图搜索答案,但所有结果都将我带到修改 XML 类本身或将元素设置为空字符串。我相信可能会有一个不错的全局切换 :)

类似于 JSON:我可以全局设置(https://www.baeldung.com/jackson-ignore-null-fields

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。