Apache CXF制作了额外的课程"已知"到JAXB上下文

时间:2017-03-28 15:41:31

标签: java jaxb cxf jax-ws

如何将Apache CXF客户端和服务器配置为在将DTO序列化为XML时将其他类传递给JAXBContext?

我不能使用@XmlSeeAlso注释,因为这些类在数据合同的jar的编译时是未知的,但在客户端编译时是已知的。

在客户端,我尝试使用:

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

但我得到例外,告诉我CXF不支持此功能。

2 个答案:

答案 0 :(得分:0)

如果使用cxf.xml(spring-xml)配置cxf,则可以使用以下命令:

<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>

或以任何其他方式编写org.apache.cxf.jaxb.JAXBDataBinding属性“extraClass”(类[])之类的。见http://cxf.apache.org/docs/jaxb.html

答案 1 :(得分:0)

您也可以使用注释。

与Spring Boot CXF入门版一起使用

@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 {
    //...
}

我想通了

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