5

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

使用 CXF 2.6.x 这很好。但是,当我在 CXF 3.x(捆绑在 TomEE 7.x 中)下调用 WS 方法时,服务器端会抛出以下异常:

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 

到目前为止,我已经阅读了有关此问题的官方CXF 文档,查看了TomEE网站上的 HowTo 并阅读了论坛中许多相关但较旧的帖子。

openejb-jar.xml我尝试通过在 web 服务的文件夹中设置属性 - 正如 TomEE 文档所建议的那样WEB-INF

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

我还尝试使用更短的属性cxf.stax.maxChildElements来检查这是否会被接受,但没有成功。

为了测试/调试,我通过tomee-maven-plugin启动 TomEE 实例,因此,我尝试将maxChildElement属性设置为环境属性,如下所示:

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

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

问题

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

4

3 回答 3

4

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

1.)将以下内容放入openejb-jar.xml文件WEB-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>

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>

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>

通常,这对于尝试设置其他与 CXF 相关的属性(如CXF 安全指南的 XML 部分中所列)也很有用,特别是用于增加或减少保守的默认值。

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

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

希望这对其他人有帮助。

于 2017-07-21T20:35:33.340 回答
2

你可以试试 -Dorg.apache.cxf.stax.maxChildElements=100000 它也应该可以

于 2020-09-15T14:45:58.760 回答
1

我认为您需要使用类型(类名) java.util.Properties 的 Service 和里面的属性定义一个 resources.xml:

  1. openejb-jar.xml 会得到这个属性:

    cxf.jaxws.properties = cxfConfig

  2. resources.xml 会得到

    org.apache.cxf.stax.maxChildElements = 1

该测试以编程方式进行:https ://github.com/apache/tomee/blob/master/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/MaxChildTest.java

于 2017-07-19T12:36:02.303 回答