Lauro182
Lauro182

Reputation: 1697

CXF XJC generation force List<String> to return null instead of empty list with JABX bindings

I'm using cxf xjc plugin version 4.0 to transform a schema to a java class, but this is generating a class that when a list is null, it returns an empty list, but I need the null, so I'm using JAXB binding to try to avoid this and get null when the list is empty or null,

the object containing MyList in my schema looks like:

<xs:complexType name="SomeObject">
  <xs:sequence>
    <xs:element maxOccurs="7" minOccurs="0" name="MyList" type="SomeType"/>
    ...

And SomeType is declared as:

<xs:simpleType name="SomeType">
  <xs:restriction base="xs:string">
    <xs:minLength  value="1"/>
    <xs:maxLength  value="50"/>
    ....

JAXB is generating a List<String> with this schema for MyList, but it's automatically returning empty list instead of null, I need to figure out how to avoid this. I cannot change the schema, also cannot change generated classes.

I'm trying to use an adapter for this, using bindings:

<jxb:bindings version="3.0"
              xmlns:jxb="http://jakarta.ee/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings schemaLocation="my-schema.xsd" 
                  node="/xs:schema/xs:complexType[@name='SomeObject']/xs:sequence/xs:element[@name='MyList']">
        <jxb:property>
            <jxb:baseType>
                <jxb:javaType name="java.util.List"
                              parseMethod="com.example.NullIfEmptyListAdapter.unmarshal"
                              printMethod="com.example.NullIfEmptyListAdapter.marshal"/>
            </jxb:baseType>
        </jxb:property>
    </jxb:bindings>
</jxb:bindings>

but this makes myList to be a List<List>.

And if I change javaType name to java.lang.String, then MyList is a <List<String>, but the adapter doesn't work, because NullIfEmptyListAdapter is for <List<String>, List<String>> and Adapter1 autogenerated is for <String, String>.

So, using it as I have it right now, almost works, except it generates myList as a List<List>>.

I need it null because I'm marshalling the class to an xml string, which is what I actually need, and empties create an empty tag which is invalid for schema.

The schema is way too big to use wrappers, that's why I would like to tackle MyList directly, but I don't have that much experience on XML to Java transformations and JABX bindings and stuff, I have tried in many many ways, this is the closest to work.

Like if I can just have MyList returning null instead of empty list, would be great.

Any suggestions much apprecciated.

Upvotes: 0

Views: 42

Answers (0)

Related Questions