如何覆盖 TomEE 容器内的“org.apache.cxf.stax.maxChildElements”属性值?

MWi*_*ner 5 stax cxf xml-parsing apache-tomee tomee-7

我有一个纯粹通过在 TomEE 7 环境中运行的注释配置的 JAX-WS Web 服务端点。基本上,被调用的方法必须返回一个List<String>包含在图形数据结构中的所有节点名称。此类请求的响应可以包含更多感谢 50k 元素。

With CXF 2.6.x this worked fine. However, when I call the WS-method under CXF 3.x (bundled in TomEE 7.x), the following exception is thrown on the server side:

org.apache.cxf.interceptor.Fault: Unmarshalling Error: Maximum Number of Child Elements limit (50000) Exceeded 
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:906)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:712)
    at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:179)
    at org.apache.cxf.wsdl.interceptors.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:109)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:801)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1680)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1559)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1356)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:653)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:423)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:324)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:277)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:139)
    at com.sun.proxy.$Proxy51.getAllNodeNames(Unknown Source)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.stream.XMLStreamException: Maximum Number of Child Elements limit (50000) Exceeded]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:485)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:417)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:394)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.doUnmarshal(JAXBEncoderDecoder.java:855)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.access$100(JAXBEncoderDecoder.java:102)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder$2.run(JAXBEncoderDecoder.java:894)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:892)
    ... 21 more
Caused by: javax.xml.stream.XMLStreamException: Maximum Number of Child Elements limit (50000) Exceeded
    at com.ctc.wstx.sr.InputElementStack.push(InputElementStack.java:340)
    at com.ctc.wstx.sr.BasicStreamReader.handleStartElem(BasicStreamReader.java:2951)
    at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2839)
    at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1073)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:196)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
    ... 27 more
Error: Maximum Number of Child Elements limit (50000) Exceeded 
Run Code Online (Sandbox Code Playgroud)

So far, I've read the official CXF documentation on this issue, checked a HowTo at the TomEE website and read many related, yet older posts in forums.

I tried to set the properties - as advised by the TomEE documentation - via openejb-jar.xml in the webservice's WEB-INF folder:

<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar>
 <ejb-deployment ejb-name="MyWebService">
   <properties>
     org.apache.cxf.stax.maxChildElements = 100000
   </properties>
 </ejb-deployment>
</openejb-jar>
Run Code Online (Sandbox Code Playgroud)

I also tried with the shorter property cxf.stax.maxChildElements to check whether this would be accepted, yet without success.

For testing/debugging, I start the TomEE instance via the tomee-maven-plugin, Therefore, I tried the set the maxChildElement property as an environment property like so:

<plugin>
    <groupId>org.apache.tomee.maven</groupId>
    <artifactId>tomee-maven-plugin</artifactId>
    <version>${tomee.plugin.version}</version>
    <configuration>
        <tomeeVersion>${tomee.version}</tomeeVersion>
        <tomeeClassifier>plus</tomeeClassifier>
        <debug>false</debug>
        <tomeeHttpPort>8181</tomeeHttpPort>
        <debugPort>5005</debugPort>
        <args>-Dfoo=bar</args>
        <skipCurrentProject>true</skipCurrentProject>
        <webapps>
            <webapp>my.ws:${webservice.artifact.name}:${webservice.artifact.version}?name=ws-endpoint</webapp>
        </webapps>
        <libs>
            <!-- Third party libraries needed in the global lib folder of TomEE -->
            <lib>log4j:log4j:${log4j.version}</lib>
        </libs>
        <systemVariables>
            <!--
                special property needed to allow for more childElements in StAX Parser
            -->
            <org.apache.cxf.stax.maxChildElement>100000</org.apache.cxf.stax.maxChildElement>
        </systemVariables>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

遗憾的是,它对 CXF/StAX (Woodstox) 的运行时配置没有影响。

我们如何在 TomEE 启动时maxChildElements通过配置openejb-jar.xml或作为外部属性覆盖该属性。

MWi*_*ner 4

最后,我在Romain Manni-Bucau的帮助下成功了(感谢他为我指明了正确的方向)。然而,他最初的答案并不是最终的解决方案。因此,我在这里给出工作配置。

1.) 将以下内容放入文件夹openejb-jar.xmlWEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar>
    <ejb-deployment ejb-name="MyWebService">
        <properties>
            cxf.jaxws.properties = cxfConfig
        </properties>
    </ejb-deployment>
</openejb-jar>
Run Code Online (Sandbox Code Playgroud)

resources.xml2.)再次通过以下方式提供新的(或添加到现有的)文件WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <Service id="cxfConfig" class-name="org.apache.openejb.config.sys.MapFactory" factory-name="create">
        org.apache.cxf.stax.maxChildElements = 100000
    </Service>
</resources>
Run Code Online (Sandbox Code Playgroud)

MapFactory请注意通过带有 id 的对象的配置链接cxfConfig

3.) 配置 JAX-WS 客户端以设置相应的属性。例如,给定一个Spring客户端,可以这样配置:

<bean id="wsClientProxy" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="com.acme.ws.jaxb.MyWebservice"/>
    <property name="address" value="${ws.endpoint.url}"/>
    <property name="properties">
        <map>
            <entry key="org.apache.cxf.stax.maxChildElements" value="100000" />
        </map>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

一般来说,这对于尝试设置CXF 安全指南 XML 部分中列出的其他 CXF 相关属性的人也可能很有用,特别是增加或减少保守的默认值。

我在 TomEE 7.0.3 和 8.0.9 环境下成功测试了上述配置步骤,但这也应该可靠地适用于所有 7.0.x 和 8.0.x 版本。

对于其他用例,Romain 的这篇博客文章也可能值得阅读,因为它很好地涵盖了基本配置概念。

希望这对其他人有帮助。