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

时间:2016-12-08 20:52:00

标签: 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>

我需要像上面那样生成故障对象。我能够生成&#39; confirmIdentityErrorInfo&#39;但无法添加子元素。我尝试了这个选项,但我无法生成嵌套对象结构。

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

}

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();
        }

    }

}

但我不确定如何将子元素添加到QName。有人可以帮助我如何产生如上所示的故障细节吗?

2 个答案:

答案 0 :(得分:1)

这是解决方案。

fault.addFaultDetail();
   this.marshaller.marshal(((AJAXB2MarshalledBusinessException)ex).getMyJAXB2MarshallableComplexMessage(), fault.getFaultDetail().getResult());

link中详细说明:

答案 1 :(得分:0)

public class DetailSoapFaultDefinitionExceptionResolver extends SoapFaultMappingExceptionResolver {
  private static final ObjectFactory FACTORY = new ObjectFactory();
  private final Marshaller marshaller;

  public DetailSoapFaultDefinitionExceptionResolver() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance("your.schema");
    this.marshaller = jaxbContext.createMarshaller();
  }

  @Override
  protected void customizeFault(Object endpoint, Exception e, SoapFault fault) {

    YourFault xsdFault = new YourFault();
    xsdFault.setCode("UNKNOWN_FAILURE");
    xsdFault.setDescription(e.getMessage());

    SoapFaultDetail faultDetail = fault.addFaultDetail();
    try {
      marshaller.marshal(FACTORY.createSubscriptionManagementFault(xsdFault), faultDetail.getResult());
    } catch (JAXBException e1) {
      log.error("Could not marshall SubscriptionManagementFault", e);
    }

  }

这里是详细说明 https://maarten.mulders.it/2018/07/custom-soap-faults-using-spring-ws/