java - CXF JAX-WS 无法将参数发送到 Web 服务

标签 java web-services jaxb cxf

当调用尝试运行使用 wsdl2java 生成的 CXF 客户端时,出现以下异常: com.sun.istack.SAXException2: Instance of "us.ak.state.labor.service.IsValidEmployerByFein" is substituting "java.lang.Object", but "us.ak.state.labor.service.IsValidEmployerByFein" is bound to an anonymous type.

我可以使用 SoapUI 成功运行 WebService。

WSDL 的相关部分如下所示:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
                  xmlns:tns="http://tempuri.org/EmployerService/Service"
                  xmlns:s="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
                  targetNamespace="http://tempuri.org/EmployerService/Service"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified"
              targetNamespace="http://tempuri.org/EmployerService/Service">
      <s:element name="IsValidEmployerByFein">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="FEIN" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
            <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="IsValidEmployerByFeinResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1"
                       name="IsValidEmployerByFeinResult">
              <s:complexType mixed="true">
                <s:sequence>
                  <s:any />
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="IsValidEmployerByFeinSoapIn">
    <wsdl:part name="parameters" element="tns:IsValidEmployerByFein" />
  </wsdl:message>
  <wsdl:message name="IsValidEmployerByFeinSoapOut">
    <wsdl:part name="parameters" element="tns:IsValidEmployerByFeinResponse" />
  </wsdl:message>
  <wsdl:portType name="ServiceSoap">
    <wsdl:operation name="IsValidEmployerByFein">
      <wsdl:input message="tns:IsValidEmployerByFeinSoapIn" />
      <wsdl:output message="tns:IsValidEmployerByFeinSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="IsValidEmployerByFein">
      <soap:operation style="document"
        soapAction="http://tempuri.org/EmployerService/Service/IsValidEmployerByFein"/>
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="IsValidEmployerByFein">
      <soap12:operation style="document"
        soapAction="http://tempuri.org/EmployerService/Service/IsValidEmployerByFein"/>
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Service">
    <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
      <soap:address location="https://my.server.name/EmployerService/Service.asmx" />
    </wsdl:port>
    <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
      <soap12:address location="https://my.server.name/EmployerService/Service.asmx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

参数类的声明如下。这些属性只是标准的字符串 getters/setters。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "fein", "username", "password" })
@XmlRootElement(name = "IsValidEmployerByFein")
public class IsValidEmployerByFein

我的ServiceSoap接口(interface)定义方法如下:

@WebService(targetNamespace = "http://tempuri.org/EmployerService/Service",
            name = "ServiceSoap")
@XmlSeeAlso({ObjectFactory.class})
public interface ServiceSoap
{
  @WebResult(name = "IsValidEmployerByFeinResult",
             targetNamespace = "http://tempuri.org/EmployerService/Service")
  @RequestWrapper(localName = "IsValidEmployerByFein",
                  targetNamespace = "http://tempuri.org/EmployerService/Service",
                  className = "us.ak.state.labor.service.IsValidEmployerByFein")
  @WebMethod(operationName = "IsValidEmployerByFein",
             action = "http://tempuri.org/EmployerService/Service/IsValidEmployerByFein")
  @ResponseWrapper(localName = "IsValidEmployerByFeinResponse",
                   targetNamespace = "http://tempuri.org/EmployerService/Service",
                   className = "us.ak.state.labor.service.isValidEmployerByFein.IsValidEmployerByFeinType")
  public IsValidEmployerByFeinType isValidEmployerByFein(
         @WebParam(name = "FEIN",
                   targetNamespace = "http://tempuri.org/EmployerService/Service")
         String fein,
         @WebParam(name = "Username",
                   targetNamespace = "http://tempuri.org/EmployerService/Service")
         String username,
         @WebParam(name = "Password",
                   targetNamespace = "http://tempuri.org/EmployerService/Service")
         String password
         );

还有我的ObjectFactory

@XmlRegistry
public class ObjectFactory
{
   public IsValidEmployerByFein createIsValidEmployerByFein()
   { return new IsValidEmployerByFein(); }

所以给予就是一切的定义方式;为什么我会得到 SAXException2它无法替代IsValidEmployerByFein ,更重要的是我该如何解决它?

最佳答案

事实证明答案非常简单。一旦我想通了。 我必须更改 @XmlType(name="", 并添加标签的名称。

现在参数类的定义如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "IsValidEmployerByFein",
         propOrder = { "fein", "username", "password" })
@XmlRootElement(name = "IsValidEmployerByFein")
public class IsValidEmployerByFein

关于java - CXF JAX-WS 无法将参数发送到 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18666070/

相关文章:

java - Spring 如何在运行时获取有关 "Strongly-typed collection"的通用类型信息?

java - 使用 JAXB 解码具有不同/动态名称的元素

java - JAXB IDREF 和 ID 用法?

java - JAXB 将空的 int XML 属性设置为 0

java - 当我输入 "java char *"时,为什么程序引用同一目录中的第一个文件?

javax.xml.bind.UnmarshalException : unexpected element (uri:

java - 为什么我的单元测试在添加参数后失败?

web-services - Get 和 post 方法与 HTTP 和 REST 的区别

c# - 从网络服务写入文件

c# - 停止向外部发布到 Asp.Net WebForm 页面的能力