0

Question

How can I inject a bean containing a list generated by JAXB ?

Detail

  • These lists have no setters.
  • You populate them through getMyList().getList().add(stuff); For standard java Collections, you usually rely on spring-utils, but Spring does not support these JAXB lists.
    Message: no matching editors or conversion strategy found

Context

  • WSDL-first - CXF server
  • mock responses are pulled from Spring Application context files

Hints

I'm reluctant to introduce a second JAXB runtime just for the sake of mock response, especially considering this will involve generating a slew of new classes to model my domain objects (i.e. thereby duplicating the objects generated by wsdl2java).

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
  • 1
    You can remove the inner list Class MyList by using XmlElementWrapper annotation. See here: **http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html**. This way you have a normal injectable java list. Here's a List definition through Spring example: **http://stackoverflow.com/questions/2416056/how-to-define-a-list-bean-in-spring** – W Almir Aug 07 '13 at 11:36
  • @WA, thx. I'm aware of this XmlElementWrapper but I'm in WSDL first and I'd rather not use exotic plugins. All the JAXB anotations in my domain objects are created by CXF wsdl2java. ATM, I'm lookin at spring oxm manual unmarshalling. Custom Spring converters might also be another lead. Thanks anyway. – Alain Pannetier Aug 07 '13 at 12:26

2 Answers2

0

Try creating a Bean for the list:

<bean id="list" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <ref bean="element1" />
            <ref bean="element2" />                
        </list>
    </constructor-arg>
</bean>

Then creating a bean for the inner class using the A$B syntax:

<bean id="myList" class="myPackage.MyOuterClass$MyList"  >
    <property name="list" ref="list" />
</bean>

Finally the OuterClass Bean:

<bean id="myOuterClass" class="myPackage.MyOuterClass"  >
    <property name="myList" ref="myList" />
</bean>
W Almir
  • 656
  • 8
  • 19
0

My Solution:

I ended up using eclipselink MOXy.

The choice of MOXy was driven by the following characteristics

  • MOXy allows to declare root elements outside of the classes generated by CXF. So that there is no need to interfere with these classes.

  • MOXy being a JAXB implementation has no problem dealing with the way JAXB populates lists (without setters).

Remarks:

  • MOXy XPath support is still weak. I needed to access a specific XML element (a response) out of the total XML file (the list of possible mock responses) and hoped I could unmarshal only a portion of this XML file based on an XPath predicate but this is not supported yet (in 2.5). Support is planned for 2.6.

  • I did not use any Spring JAXB front-end as a façade for MOXy, as my Mock SEI are already injected through Spring.

  • Using MOXy proved a pleasant experience and is quite easy to get started with.

  • I did not experience any collision between MOXy as a JAXB payload jar and the way CXF uses the JDK JAXB implementation for its SOAP layer.

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46