Xsd: custom type list is created in List <String>

We have an xsd schema with a declaration like this:

<xsd:simpleType name="customId"> <xsd:annotation> <xsd:appinfo> <jaxb:javaType name="com.company.identifiers.CustomId" parseMethod="fromString" printMethod="toString"/> </xsd:appinfo> </xsd:annotation> <xsd:restriction base="xsd:int" /> </xsd:simpleType> 

Then I want to have a list of this type in the generated Java class:

 <xsd:complexType name="SomeMessage"> ... <xsd:attribute name="customIds" use="optional"> <xsd:simpleType> <xsd:list itemType="customId" /> </xsd:simpleType> </xsd:attribute> ... </xsd:complexType> 

But the customIds field customIds for some reason generated as a List<String> .

I assume that xsd:sequence can be used instead of xsd:list , but SomeMessage already has xsd:choice , and as far as I understand, it is illegal to have xsd:sequence in the same declaration.

Thanks!

+6
source share
1 answer

Code generated using NetBeans 7.1.2, running on Java 1.7.0_02.

If you want to map simple types to Java classes, one way to do this is to globally set mapSimpleTypeDef="true"

 <xsd:annotation> <xsd:appinfo> <jaxb:globalBindings mapSimpleTypeDef="true"/> </xsd:appinfo> </xsd:annotation> 

Generated Code:

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "SomeMessage") public class SomeMessage { @XmlAttribute(name = "customIds") protected List<CustomId> customIds; /** * Gets the value of the customIds property. * * <p> * Objects of the following type(s) are allowed in the list * {@link CustomId } * * */ public List<CustomId> getCustomIds() { if (customIds == null) { customIds = new ArrayList<CustomId>(); } return this.customIds; } } 

If you want to send your previously existing CustomId class, in my case the following works:

 <xsd:simpleType name="customId"> <xsd:restriction base="xsd:int"/> </xsd:simpleType> <xsd:complexType name="SomeMessage"> <xsd:attribute name="customIds" use="optional"> <xsd:simpleType> <xsd:annotation> <xsd:appinfo> <jaxb:javaType name="java.util.List&lt;com.company.identifiers.CustomId>" parseMethod="Class1.fromString" printMethod="Class1.toString"/> </xsd:appinfo> </xsd:annotation> <xsd:list itemType="customId"/> </xsd:simpleType> </xsd:attribute> </xsd:complexType> 

You will receive the following:

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "SomeMessage") public class SomeMessage { @XmlAttribute(name = "customIds") @XmlJavaTypeAdapter(Adapter1 .class) protected List<CustomId> customIds; /** * Gets the value of the customIds property. * * @return * possible object is * {@link String } * */ public List<CustomId> getCustomIds() { return customIds; } /** * Sets the value of the customIds property. * * @param value * allowed object is * {@link String } * */ public void setCustomIds(List<CustomId> value) { this.customIds = value; } } 

And the generated Adapter1:

 public class Adapter1 extends XmlAdapter<String, List<CustomId>> { public List<CustomId> unmarshal(String value) { return (Class1.fromString(value)); } public String marshal(List<CustomId> value) { return (Class1.toString(value)); } } 
+3
source

Source: https://habr.com/ru/post/926102/


All Articles