java - XSD 中的哪个属性将在 JAXB 生成的类中使用 @XmlElement(required = false) 注释字段

标签 java jaxb xsd jaxb2 jaxb2-maven-plugin

我想使用此注释 - @XmlElement(required = false) 对 jaxb 生成的类中的字段进行注释。 XSD 中的哪个属性会生成带有此注释的字段? 我无法手动输入此内容,因为每次运行构建时都会使用 Maven 自动生成 JAXB 类。

我的jaxb版本是xjc 2.2.4-2

谢谢

最佳答案

当元素具有 minOccurs="0" 时,相应的 @XmlElement 具有 required=false。请注意,falserequired 属性的默认值,因此它实际上可能不会出现在生成的注释中。

<小时/>

更新

根据您的评论:

Let me explain my actual problem. I'm using Jackson to generate the JSON from the JAXB classes. Issue is when the element is not present in the xml, I see the json output with the field name as 'pip' and value as null. I am actually expecting the field 'pip' to be absent from my json output as I declared it to be minOccurs=0 in the XSD. Can't figure out if it's an issue with JAXB or Jackson. Interestingly when I annotate the field explicitly with required=false in the jaxb class, I see my expected output with the field being absent

这是 Jackson 未正确处理 @XmlElement 注释上 required 属性的默认值的问题。

关于java - XSD 中的哪个属性将在 JAXB 生成的类中使用 @XmlElement(required = false) 注释字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23082354/

相关文章:

java - RecyclerView 项目内容混淆

java - 如何限制用户只能输入一个整数?

java - JAXB 是否总是忽略@XmlType/propOrder 中未指定的 'extra' 元素?

java - TargetNamespace 与 Namespace 有何不同

java - 如何更改此正则表达式以正确提取标签属性 - 应该很简单

java - 从 STDIN 读取命令并在输入时执行每个命令

java - 如果字段名称包含一个大写字母后跟下划线,则从 JAXB 带注释的类生成模式会失败

java - JAXB 创建空对象

无序列和多次出现的 XML 模式

java - 使用 xjc 和绑定(bind)为通用 XSD 定义包名称