3

How to configure Apache CXF client and server to pass additional classes to JAXBContext when it is serializing DTO to XML?

I can't use @XmlSeeAlso annotations because those classes are not known at compile time of jar with data contracts, but known when client compiles.

On client side I tried using:

Service service = Service.create(wsdlURL, serviceName, new UsesJAXBContextFeature(MyFactory.class));
T client = service.getPort(clazz);

But I got exception telling me that CXF doesn't support this feature.

Alex Burtsev
  • 12,418
  • 8
  • 60
  • 87

2 Answers2

2

You can do it with annotations also.

Works with Spring Boot CXF starter

@Autowired
private Bus bus;

@Bean
public Endpoint createMyEndpoint() {

    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    Map<String, Object> properties = new HashMap<>();
    properties.put("jaxb.additionalContextClasses", getExtraClasses());
    factory.setProperties(properties);

    Endpoint endpoint = new EndpointImpl(bus, new MyWebService(),factory);
    endpoint.setProperties(new HashMap<>());
    endpoint.publish("/v1/service");
    return endpoint;
}

@SuppressWarnings("rawtypes")
private Class[] getExtraClasses() {
    List<Class> extraClassList = new ArrayList<>();

    extraClassList.add(A.class);
    extraClassList.add(B.class);

    return  extraClassList.toArray(new Class[extraClassList.size()]);
}

...

@javax.jws.WebService
public class MyWebService implements MyPortType {
    //...
}

I figured it out with

https://issues.apache.org/jira/browse/CXF-340

https://github.com/apache/cxf/blob/5578e0b82bcd4ea19c1de5b4a008af35f9c8451b/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/EndpointImpl.java#L164

Filip
  • 906
  • 3
  • 11
  • 33
0

if you configure cxf with cxf.xml (spring-xml) you can use the following:

<jaxws:endpoint/client>
  <jaxws:properties>
    <entry key="jaxb.additionalContextClasses">
      <array value-type="java.lang.Class">
          <value type="java.lang.Class">fullQualifiedClassName</value>
      </array>
    </entry>
   </jaxws:properties>
</jaxws:endpoint>

or any other way to write the org.apache.cxf.jaxb.JAXBDataBinding property "extraClass" (a Class[]) like . See http://cxf.apache.org/docs/jaxb.html

Markus Schulz
  • 500
  • 5
  • 12