通过Apache CXF 2.x为soap客户端生成代码时出现NoSuchMethodError

时间:2016-06-11 21:11:22

标签: java web-services soap cxf wsdl2java

当我为SOAP客户端生成所需的类时,通过Apache CXF 2.x WSDL2Java,我得到了这个异常。任何帮助深表感谢。谢谢!

  

线程中的异常" main" java.lang.NoSuchMethodError:com.sun.xml.bind.v2.util.XmlFactory.allowExternalAccess(Ljavax / xml / validation / SchemaFactory; Ljava / lang / String; Z)Ljavax / xml / validation / SchemaFactory;       在com.sun.tools.xjc.reader.internalizer.DOMForest.weakSchemaCorrectnessCheck(DOMForest.java:478)       at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:260)       在com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:95)       在org.apache.cxf.tools.wsdlto.databinding.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:460)       at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.generateTypes(WSDLToJavaContainer.java:727)       at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.processWsdl(WSDLToJavaContainer.java:271)       在org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:165)       在org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:416)       在org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:103)       在org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:113)       在org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:86)       在org.apache.cxf.tools.wsdlto.WSDLToJava.main(WSDLToJava.java:184)

WSDL

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:tns="http://www.example.org/Calculator/" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
name="Calculator" 
targetNamespace="http://www.example.org/Calculator/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://www.example.org/Calculator/">
      <xsd:element name="Add">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element minOccurs="0" name="a" type="xsd:int" />
            <xsd:element minOccurs="0" name="b" type="xsd:int" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="AddResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element minOccurs="0" name="result" type="xsd:int" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="Subtract">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="0" name="a" type="xsd:int" />
                <xsd:element minOccurs="0" name="b" type="xsd:int" />
            </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="SubtractResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="0" name="result" type="xsd:int" />
            </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="AddRequest">
    <wsdl:part element="tns:Add" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="AddResponse">
    <wsdl:part element="tns:AddResponse" name="parameters"/>
  </wsdl:message>
  <wsdl:message name="SubtractRequest">
    <wsdl:part name="parameters" element="tns:Subtract"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="SubtractResponse">
    <wsdl:part name="parameters" element="tns:SubtractResponse"></wsdl:part>
  </wsdl:message>
  <wsdl:portType name="Calculator">
    <wsdl:operation name="Add">
      <wsdl:input message="tns:AddRequest"/>
      <wsdl:output message="tns:AddResponse"/>
    </wsdl:operation>
    <wsdl:operation name="Subtract">
        <wsdl:input message="tns:SubtractRequest"></wsdl:input>
        <wsdl:output message="tns:SubtractResponse"></wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="CalculatorSoapBinding" type="tns:Calculator">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="Add">
      <soap:operation soapAction="http://www.example.org/Calculator/Add"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="Subtract">
      <soap:operation soapAction="http://www.example.org/Calculator/Subtract"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="CalculatorService">
    <wsdl:port binding="tns:CalculatorSoapBinding" name="CalculatorSoapPort">
      <soap:address location="http://www.example.org/Calculator"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

2 个答案:

答案 0 :(得分:0)

[在评论中解决]

JDK附带了来自java6的JAXB。确保在类路径中没有旧版本的jaxb。如果您使用CXF 2.7,请在2.7.x CXF发行版中包含stax2-api和Woodstox-core-asl jar

答案 1 :(得分:0)

您在pom.xml中使用较旧的jaxb-impl依赖。

列出了所有jaxb-impl版本here