【问题标题】:How to add complex XML to SOAP Fault detail in Spring web service?如何在 Spring Web 服务中将复杂的 XML 添加到 SOAP 故障详细信息?
【发布时间】:2016-12-08 20:52:00
【问题描述】:
<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>

我需要像上面那样生成故障对象。我能够生成“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;
    }

}


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。有人可以帮助我如何生成如上所示的故障详细信息吗?

【问题讨论】:

    标签: java spring soap soapfault


    【解决方案1】:

    这就是解决方案。

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

    更详细的在这个link

    【讨论】:

    【解决方案2】:
    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/

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多