解析使用契约优先Spring-WS完成的SOAP的异常

时间:2011-10-18 15:39:27

标签: spring xsd marshalling spring-ws castor

我正在尝试编写我的第一个SOAP Web服务。

我收到SOAP响应Unmarshall失败 这是我映射到org.springframework.oxm.UnmarshallingFailureException的响应。 我已将Spring配置为使用Castor(un)marshaller。 问题是我不知道如何找到更具体的原因。

public class SentenceRequest {

public SentenceRequest() {}

private List<String> words = new ArrayList<String>();

public List<String> getWords() {
    return words;
}

public void setWords(List<String> words) {
    this.words = words;
}


public class SentenceResponse {

private String sentence;

public SentenceResponse() {}

public SentenceResponse(String sen) {
    sentence = sen;
}

public String getSentence() {
    return sentence;
}

public void setSentence(String sentence) {
    this.sentence = sentence;
}

蓖麻映射:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapping PUBLIC
    "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN"
    "http://castor.exolab.org/mapping.dtd">
<mapping xmlns="http://castor.exolab.org/">
    <class name="ro.soapservice2.SentenceRequest">
        <map-to xml="SentenceRequest" ns-uri="sentence" ns-prefix="s" />
        <field name="words" collection="arraylist" type="java.util.List" required="true">
            <bind-xml name="word" node="element"></bind-xml>
        </field>
    </class>

    <class name="ro.soapservice2.SentenceResponse">
        <map-to xml="SentenceResponse" ns-uri="sentence" ns-prefix="s" />
        <field name="sentence" type="java.lang.String" required="true">
            <bind-xml name="sentence" node="element" />
        </field>
    </class>
</mapping>

模式(使用Trang.jar基于两个输入XML文件生成):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"      targetNamespace="sentence" xmlns:s="sentence">
  <xs:element name="SentenceRequest">
    <xs:complexType>
          <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="s:word"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
  <xs:element name="word" type="xs:string"/>
    <xs:element name="SentenceResponse">
      <xs:complexType>
        <xs:sequence>
          <xs:element ref="s:sentence"/>
        </xs:sequence>
      </xs:complexType>
     </xs:element>
   <xs:element name="sentence" type="xs:string"/>
</xs:schema>

并且WSDL Spring生成:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="sentence" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="sentence" targetNamespace="sentence">
  <wsdl:types>
    <xs:schema xmlns:s="sentence" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="sentence">

  <xs:element name="SentenceRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="s:word"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="word" type="xs:string"/>

  <xs:element name="SentenceResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="s:sentence"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="sentence" type="xs:string"/>

 </xs:schema>
</wsdl:types>

  <wsdl:message name="SentenceResponse">
    <wsdl:part element="tns:SentenceResponse" name="SentenceResponse">
    </wsdl:part>
  </wsdl:message>

  <wsdl:message name="SentenceRequest">
    <wsdl:part element="tns:SentenceRequest" name="SentenceRequest">
    </wsdl:part>
  </wsdl:message>

  <wsdl:portType name="Sentence">
    <wsdl:operation name="Sentence">
      <wsdl:input message="tns:SentenceRequest" name="SentenceRequest">
      </wsdl:input>
      <wsdl:output message="tns:SentenceResponse" name="SentenceResponse">
      </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>

  <wsdl:binding name="SentenceSoap11" type="tns:Sentence">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="Sentence">
      <soap:operation soapAction=""/>
      <wsdl:input name="SentenceRequest">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="SentenceResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="SentenceService">
    <wsdl:port binding="tns:SentenceSoap11" name="SentenceSoap11">
      <soap:address location="http://localhost:8080/soapservice2/services"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

我发出的SOAP请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="sentence">
<soapenv:Header/>
 <soapenv:Body>
    <sen:SentenceRequest>
       <!--1 or more repetitions:-->
       <sen:word>asd</sen:word>
    </sen:SentenceRequest>
 </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

解决! 首先,我从

替换了我的异常解析器
org.springframework.ws.soap.server.endpoint.SoapFaultMappingExceptionResolver

org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver

将SOAP引发的实际异常放回到SOAP响应中。 然后我看到问题出在Castor xml映射文件中:

更改
<field name="words" collection="arraylist" type="java.util.List" required="true">
                <bind-xml name="word" node="element"></bind-xml>
        </field>

<field name="words" collection="arraylist" type="java.lang.String" required="true">
                <bind-xml name="word" node="element"></bind-xml>
        </field>