Spring WS SOAP代理丢失Fault详情问题求助
SOAP代理服务器丢失Fault Detail的问题排查与解决
问题现象
使用org.springframework.ws.*构建SOAP转SOAP代理,两端WSDL完全一致。正常响应能正确转发,但外部服务返回Fault时,代理会移除原始响应中的SOAP detail部分,导致客户端无法收到完整错误信息。
外部服务器原始Fault响应
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <soap:Fault> <faultcode>soap:Server</faultcode> <faultstring>Fault occurred while processing.</faultstring> <detail> <ns2:getCFUSubscriberStateFaultBusiness xmlns="example.system.messaging.common.v1.datatypes" xmlns:ns2="example.system.ot.managepcccfu.v2.datatypes"> <ns2:messageContext> <requestId>273140800423344000</requestId> <transactionId>8200</transactionId> <timeLeft>10000</timeLeft> <timestamp>2022-09-30T14:08:00</timestamp> <user>x_turan</user> <consumingComponent>urn:example.business.intr:SELFCARE3.</consumingComponent> <providingService>urn:example.system.ot.managepccprovisioning.v1.service:ManagePccProvisioning</providingService> <providingOperation>modifycontroffer</providingOperation> </ns2:messageContext> <ns2:messageDataBusinessException> <errorCode>3001</errorCode> <errorMessage>ESE Problem</errorMessage> </ns2:messageDataBusinessException> </ns2:getCFUSubscriberStateFaultBusiness> </detail> </soap:Fault> </soap:Body> </soap:Envelope>
代理客户端收到的截断后响应
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Server</faultcode> <faultstring xml:lang="en">Fault occurred while processing.</faultstring> </SOAP-ENV:Fault> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
现有代码实现
Endpoint代码
@Endpoint public class ServiceEndpoint { public static final String NAMESPACE_URI="example.system.ot.managepcccfu.v2.datatypes"; @Autowired CFUSoapClient soapClient; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCFUSubscriberState" ) @ResponsePayload public GetCFUSubscriberStateResponse getCFUSubscriberState(@RequestPayload GetCFUSubscriberState request) throws GetCFUSubscriberStateFaultBusinessMessage, GetCFUSubscriberStateFaultSystemMessage { final GetCFUSubscriberStateResponse response = soapClient.getCFUSubscriberStateResponse(request); return response; } }
SOAP客户端代码
public class CFUSoapClient extends WebServiceGatewaySupport { public GetCFUSubscriberStateResponse getCFUSubscriberStateResponse(GetCFUSubscriberState request) throws GetCFUSubscriberStateFaultBusinessMessage { Object response = getWebServiceTemplate().marshalSendAndReceive(request); return (GetCFUSubscriberStateResponse) response; } }
配置代码
@Configuration @EnableWs public class Config extends WsConfigurerAdapter { @Bean public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(applicationContext); return new ServletRegistrationBean(servlet, "/ws/CFU/*"); } @Bean(name="CFU") public Wsdl11Definition defaultWsdl11Definition() { SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition(); wsdl11Definition.setWsdl(new ClassPathResource("/wsdl/CFU.wsdl")); return wsdl11Definition; } @Bean(name = "cfuDatatypesV2") public XsdSchema cfuDatatypesV2() { return new SimpleXsdSchema( new ClassPathResource("wsdl/cfuDatatypesV2.xsd")); } @Bean(name = "common") public XsdSchema common() { return new SimpleXsdSchema( new ClassPathResource("wsdl/common.xsd")); } @Bean public Jaxb2Marshaller marshaller() { Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); jaxb2Marshaller.setContextPath("com.ot.cfu"); return jaxb2Marshaller; } @Bean public CFUSoapClient soapClient() { CFUSoapClient client = new CFUSoapClient(); client.setDefaultUri("http://localhost:41420/CFU"); client.setMarshaller(marshaller()); client.setUnmarshaller(marshaller()); // ClientInterceptor [] interceptors = new ClientInterceptor[]{new SoapClientInterceptor()}; // client.setInterceptors(interceptors); return client; } }
问题原因与解决方法
原因分析
Spring-WS默认处理Fault时,仅将faultcode和faultstring转换为抛出的异常,不会自动携带detail中的自定义错误信息。当Endpoint抛出声明的异常时,框架生成的默认Fault响应不会包含原始detail内容。
解决步骤
1. 自定义Endpoint的Fault处理逻辑
修改Endpoint代码,捕获外部服务返回的异常,将自定义错误信息写入SoapFaultDetail后抛出,确保detail部分被正确序列化:
@Endpoint public class ServiceEndpoint { public static final String NAMESPACE_URI = "example.system.ot.managepcccfu.v2.datatypes"; @Autowired CFUSoapClient soapClient; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCFUSubscriberState") @ResponsePayload public GetCFUSubscriberStateResponse getCFUSubscriberState(@RequestPayload GetCFUSubscriberState request) throws SoapFaultException { try { return soapClient.getCFUSubscriberStateResponse(request); } catch (GetCFUSubscriberStateFaultBusinessMessage ex) { SoapFault fault = SoapFault.createServerFault(ex.getMessage(), null); SoapFaultDetail detail = fault.addFaultDetail(); try { Marshaller marshaller = soapClient.getMarshaller(); marshaller.marshal(ex.getFaultInfo(), detail.getResult()); } catch (JAXBException e) { throw new SoapFaultException("Failed to marshal fault detail", fault); } throw new SoapFaultException(fault); } catch (GetCFUSubscriberStateFaultSystemMessage ex) { SoapFault fault = SoapFault.createServerFault(ex.getMessage(), null); SoapFaultDetail detail = fault.addFaultDetail(); try { Marshaller marshaller = soapClient.getMarshaller(); marshaller.marshal(ex.getFaultInfo(), detail.getResult()); } catch (JAXBException e) { throw new SoapFaultException("Failed to marshal fault detail", fault); } throw new SoapFaultException(fault); } } }
2. 增强客户端的Fault解析能力
修改CFUSoapClient,添加自定义FaultMessageResolver,确保客户端能正确解析并携带detail中的异常信息:
public class CFUSoapClient extends WebServiceGatewaySupport { public GetCFUSubscriberStateResponse getCFUSubscriberStateResponse(GetCFUSubscriberState request) throws GetCFUSubscriberStateFaultBusinessMessage, GetCFUSubscriberStateFaultSystemMessage { WebServiceTemplate template = getWebServiceTemplate(); template.setFaultMessageResolver(new CustomFaultMessageResolver()); try { return (GetCFUSubscriberStateResponse) template.marshalSendAndReceive(request); } catch (SoapFaultClientException ex) { SoapFaultDetail detail = ex.getSoapFault().getFaultDetail(); if (detail != null) { Unmarshaller unmarshaller = getUnmarshaller(); try { Object faultInfo = unmarshaller.unmarshal(detail.getSource()); if (faultInfo instanceof GetCFUSubscriberStateFaultBusiness) { throw new GetCFUSubscriberStateFaultBusinessMessage((GetCFUSubscriberStateFaultBusiness) faultInfo); } else if (faultInfo instanceof GetCFUSubscriberStateFaultSystem) { throw new GetCFUSubscriberStateFaultSystemMessage((GetCFUSubscriberStateFaultSystem) faultInfo); } } catch (JAXBException e) { throw new RuntimeException("Failed to unmarshal fault detail", e); } } throw ex; } } private static class CustomFaultMessageResolver implements FaultMessageResolver { @Override public void resolveFault(WebServiceMessage message) throws IOException { SoapMessage soapMessage = (SoapMessage) message; SoapBody body = soapMessage.getSoapBody(); SoapFault fault = body.getFault(); throw new SoapFaultClientException(soapMessage); } } }
3. 验证JAXB绑定完整性
确保com.ot.cfu包下的JAXB生成类包含GetCFUSubscriberStateFaultBusiness和GetCFUSubscriberStateFaultSystem,这些类需由wsimport等工具基于WSDL自动生成,保证能被Marshaller/Unmarshaller正确处理。
内容的提问来源于stack exchange,提问作者mareck_ste
相关产品推荐
相关产品推荐

