如何在 Spring Web 服务中将复杂的 XML 添加到 SOAP 故障详细信息?

Lak*_*lla 5 java spring soap soapfault

<soapenv:Fault xmlns:m="http://schemas.xmlsoap.org/soap/envelope/">
     <faultcode>m:Server</faultcode>
     <faultstring>someFaultString</faultstring>
     <detail>
        <NS1:confirmIdentityErrorInfo xmlns:NS1="example.com">
           <faultInfo>
              <faultType>Business Exception</faultType>
              <faultCode>100</faultCode>
              <message>some message</message>
              <faultState>fault state</faultState>
           </faultInfo>
        </NS1:confirmIdentityErrorInfo>
     </detail>
  </soapenv:Fault>
Run Code Online (Sandbox Code Playgroud)

我需要如上所述生成故障对象。我能够生成“confirmIdentityErrorInfo”,但无法添加子元素。我尝试了这个选项,但无法生成嵌套对象结构。

public class WebServiceConfig extends WsConfigurerAdapter {

 @Bean
    public SoapFaultMappingExceptionResolver exceptionResolver(){
        SoapFaultMappingExceptionResolver exceptionResolver = new DetailSoapFaultDefinitionExceptionResolver();

        SoapFaultDefinition faultDefinition = new SoapFaultDefinition();
        faultDefinition.setFaultCode(SoapFaultDefinition.SERVER);
        exceptionResolver.setDefaultFault(faultDefinition);

        Properties errorMappings = new Properties();
        errorMappings.setProperty(Exception.class.getName(), SoapFaultDefinition.SERVER.toString());
        errorMappings.setProperty(BusinessException.class.getName(), SoapFaultDefinition.SERVER.toString());
        exceptionResolver.setExceptionMappings(errorMappings);
        exceptionResolver.setOrder(1);
        return exceptionResolver;
    }
Run Code Online (Sandbox Code Playgroud)

}


protected void customizeFault(Object endpoint, Exception ex, SoapFault fault) {
    if (ex instanceof BusinessException) {
        SoapFaultDetail detail = fault.addFaultDetail();
        try
        {
            QName entryName = new QName("namespace", "confirmIdentityErrorInfo", "NS1");
            detail.addFaultDetailElement(entryName);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何向 QName 添加子元素。有人可以帮助我如何生成如上所示的故障详细信息吗?

Lak*_*lla 4

这就是解决方案。

fault.addFaultDetail();
   this.marshaller.marshal(((AJAXB2MarshalledBusinessException)ex).getMyJAXB2MarshallableComplexMessage(), fault.getFaultDetail().getResult());
Run Code Online (Sandbox Code Playgroud)

更详细的信息请参见此链接: