JAXB-在xsd:all内部具有不受限制的maxOccur的元素 [英] JAXB - an element with unbounded maxOccurs inside an xsd:all
问题描述
假设我有以下java类,它映射到我的REST服务的请求主体以搜索用户.
Suppose I have the following java class that maps to the request-body of my REST service to search users.
@XmlRootElement(name = "SearchParams")
@XmlType(propOrder = {})
public class SearchParams {
private String firstname;
private String lastname;
private List<String> role;
...
}
请注意 @XmlType 批注的 propOrder 参数-它声明表示单个属性在XML文件中出现的顺序无关紧要,因此 xsd:all 代替 xsd:sequence .
Note the propOrder parameter of the @XmlType annotation - it declares that the order in which representing individual attributes appear in an XML file does not matter and thus an xsd:all should be used instead of xsd:sequence in the generated XSD schema.
但是,正如您所看到的,其中一个属性( role 属性)是一个列表,因此与具有 unbounded maxOccurs 的元素相对应.
However, as you can see, one of the attributes (the role attribute) is a list and thus corresponds to an element with an unbounded maxOccurs.
xsd:all complexType 中似乎不允许具有 maxOcbounds 的元素.如何避免这个问题?
It seems that an element with an unbounded maxOccurs is not allowed within an xsd:all complexType. How do I avoid this problem?
请注意,与角色不同,名字和姓氏的 maxOccurs 为 1 .因此,我也不能使用 unbounded xsd:choice 代替 xsd:all .
Note that, unlike role, the firstname and lastname have maxOccurs of 1. I therefore cannot use an unbounded xsd:choice instead of xsd:all either.
推荐答案
假设您仍然可以修改此模型,则可以将角色列表包装为自己的类型,因此最终会得到一个<roles>
元素,并带有一个<role>
s
Assuming you can still modify this model, you could just wrap the role list in its own type, so you would end up with a <roles>
element with a list of <role>
s
<SearchParams>
<firstname>firstname</firstname>
<lastname>lastname</lastname>
<roles>
<role>role</role>
</roles>
</SearchParams>
xsd就是
<xsd:schema ... >
<xsd:element name="SearchParams">
<xsd:complexType>
<xsd:all>
<xsd:element name="firstname" type="xsd:string" />
<xsd:element name="lastname" type="xsd:string" />
<xsd:element name="roles" type="Roles" />
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="Roles">
<xsd:sequence>
<xsd:element name="role" type="xsd:string" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
您将有两个班级.包含列表的Roles
类.海事组织,这是一种更清洁的方法. (您可以只将Roles
设为匿名,但随后您将陷入一个静态内部类中)
And you'd have two classes. The Roles
class containing the list. IMO, this is a cleaner approach. (you could just make Roles
anonymous, but then you'd be stuck with a static inner class)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
})
@XmlRootElement(name = "SearchParams")
public class SearchParams {
@XmlElement(required = true)
protected String firstname;
@XmlElement(required = true)
protected String lastname;
@XmlElement(required = true)
protected Roles roles;
...
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Roles", propOrder = {
"role"
})
public class Roles {
@XmlElement(required = true)
protected List<String> role;
...
}
更新
如果我可以通过这种方式更改模型类,那么看起来这是解决此问题的好方法.不幸的是,我只允许更改批注"
"If I could change the model class this way than this would look like a great way to solve this issue. Unfortunately I am only allowed to change the annotations"
您可以使用 @XmlList
.
You could use @XmlList
.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
})
@XmlRootElement(name = "SearchParams")
public class SearchParams {
@XmlElement(required = true)
protected String firstname;
@XmlElement(required = true)
protected String lastname;
@XmlList
@XmlElement(required = true)
protected List<String> role;
...
}
xsd看起来像
<xsd:element name="SearchParams">
<xsd:complexType>
<xsd:all>
<xsd:element name="firstname" type="xsd:string" />
<xsd:element name="lastname" type="xsd:string" />
<xsd:element name="role">
<xsd:simpleType>
<xsd:list itemType="xsd:string" />
</xsd:simpleType>
</xsd:element>
</xsd:all>
</xsd:complexType>
</xsd:element>
注意:如前所述list相比,本文开头的解决方案更可取.但是在您的特定情况下,不确定我是否看到其他方式
Note: As noted here, this method is a "dangerous", the solution in the beginning of my post is preferred over using a list
of string types. But in your specific case, not sure if I see another way
这篇关于JAXB-在xsd:all内部具有不受限制的maxOccur的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!