[英]Modifying incoming webservice response with CXF transform feature
I am making a call to an external webservice and the faultcode that is returned seems to be causing some trouble for CXF.我正在调用外部网络服务,返回的故障代码似乎给 CXF 造成了一些麻烦。 The response is as follows :
响应如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>S:Server</faultcode><faultstring>Something serious wen wrong</faultstring></soap:Fault></soap:Body></soap:Envelope>
CXF has trouble with this because of the S:Server not being valid.由于 S:Server 无效,CXF 遇到了这个问题。 The resulting error is
由此产生的错误是
java.lang.RuntimeException: Invalid QName in mapping: S:Server
I am trying to use the CXF Transform feature to change the faultcode but I cannot seem to get it working correctly.我正在尝试使用 CXF 转换功能来更改故障代码,但我似乎无法使其正常工作。 The desired output should be:
所需的输出应该是:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>Something serious wen wrong</faultstring></soap:Fault></soap:Body></soap:Envelope>
I have had a look at CXF Transform Feature and tried the following in my code :我看过CXF 转换功能并在我的代码中尝试了以下内容:
BindingProvider bp = (BindingProvider) port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
Client client = ClientProxy.getClient(port);
Map<String, String> inTransformMap = Collections.singletonMap("faultcode", "faultcode=soap:Server");
org.apache.cxf.feature.StaxTransformFeature staxTransformFeature = new org.apache.cxf.feature.StaxTransformFeature();
staxTransformFeature.setInAppendElements(inTransformMap);
staxTransformFeature.initialize(client, client.getBus());
This does not seem to replace the faultcode.这似乎不能代替故障代码。 I have also tried other variations like
我还尝试了其他变体,例如
I am unable to find any clear examples showing the usage of the transform feature (eg. input and output xml)我找不到任何显示转换功能使用的清晰示例(例如输入和输出 xml)
Please guide me on what I may be doing wrong.请指导我我可能做错了什么。
Thanks in advance.提前致谢。
I have resorted to creating an Interceptor to solve my problem (with the help of this ).我已经求助于创建一个拦截器来解决我的问题(在this的帮助下)。
public class ChangeSoapFaultCodeInterceptor extends AbstractPhaseInterceptor<Message> {
public ChangeSoapFaultCodeInterceptor() {
super(Phase.RECEIVE);
}
@Override
public void handleMessage(Message message) throws Fault {
boolean isInbound = message == message.getExchange().getInMessage() || message == message.getExchange().getInFaultMessage();
if (isInbound) {
try (InputStream is = message.getContent(InputStream.class);) {
if (is != null) {
byte[] rawContent = ByteStreams.toByteArray(is);
String content = new String(rawContent, StandardCharsets.UTF_8);
String cleanContent = modifySoapFaultCode(content);
message.setContent(InputStream.class, new ByteArrayInputStream(cleanContent.getBytes(StandardCharsets.UTF_8)));
}
} catch (IOException e) {
throw new Fault(e);
}
}
}
private static String modifySoapFaultCode(String message) {
message = message.replaceAll("<faultcode>([^<]*)</faultcode>", "<faultcode>Server</faultcode>");
System.out.println("After change message is " + message);
return message;
}
} }
Then add the interceptor to the chain :然后将拦截器添加到链中:
BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
Client client = ClientProxy.getClient(port);
client.getInInterceptors().add(new ChangeSoapFaultCodeInterceptor());
return port.sayRuntimeException();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.