java - 定制肥皂响应
问题描述
我正在使用 Apache CXF 开发 Web 服务项目。我不想处理异常并自定义响应:
public class FaultInterceptor extends
AbstractSoapInterceptor {
public FaultInterceptor() {
super(Phase.MARSHAL);
}
public void handleMessage(SoapMessage soapMessage) throws Fault {
Fault fault = (Fault) soapMessage.getContent(Exception.class);
QName faultCode = new QName("11111");
fault.setFaultCode(faultCode);
所以这是我在回复中得到的:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:11111</faultcode>
<faultstring>Message</faultstring>
</soap:Fault>
</soap:Body>
我怎样才能删除文本“肥皂:”,只让 11111?
请帮助我,并在此先感谢
解决方案
自从迁移旧系统以使其行为完全相同以来,我一直在寻找完全相同的东西。
我想出了以下解决方案。
class SoapFaultEndpointInterceptor extends EndpointInterceptorAdapter
{
private static final Pattern SOAP_CODE_FAULT_SPLITTER = Pattern.compile(":");
@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception
{
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
modifySoapFaultCode(soapResponse);
return super.handleFault(messageContext, endpoint);
}
private void modifySoapFaultCode(SaajSoapMessage soapResponse)
{
try {
SOAPMessage soapMessage = soapResponse.getSaajMessage();
SOAPBody body = soapMessage.getSOAPBody();
SOAPFault soapFault = body.getFault();
modifyFaultCodeIfPresent(soapFault);
} catch (SOAPException e) {
throw new SoapModifiyingException("Modifying faultcode did not work properly.", e);
}
}
private void modifyFaultCodeIfPresent(SOAPFault fault)
{
if (fault != null) {
String newFaultCode = cleanFaultCode(fault.getFaultCode());
fault.setFaultCode(newFaultCode);
}
}
private String cleanFaultCode(String oldFaultCode)
{
String[] cleanFaultCode = SOAP_CODE_FAULT_SPLITTER.split(oldFaultCode);
Assert.isTrue(cleanFaultCode.length == 2, "No proper faultcode provided!");
return cleanFaultCode[1].trim();
}
通过添加SoapFaultEndpointInterceptor
到您的拦截器,它应该可以工作。
@EnableWs
@Configuration
public class SoapServerConfig extends WsConfigurerAdapter
{
@Override
public void addInterceptors(List<EndpointInterceptor> interceptors)
{
interceptors.add(new SoapFaultEndpointInterceptor());
}
}
推荐阅读
- android - 如何在本机反应中检测附加/分离的外围设备?
- javascript - Angular将多个文件更改为内联模板
- reactjs - 生产中的 REACT 状态在 DevTools 中是否可见?
- flutter - 如何从streambuilder中的websocket解压缩数据(由zstd压缩)?
- kubernetes - 是否可以部署一个在本地运行一项服务的集群?
- flexbox - Twilio Flex 在 Salesforce 中未正确显示
- android - 从另一个应用程序在 android 10 中启动应用程序
- python - 定义python3执行命令时不能在zsh mac上使用别名
- arrays - 获取 Firebase JSON 并将其存储在 ESP32 中的数组中
- button - 创建浮动按钮以在电子表格中的单元格中添加/减去值