gpt4 book ai didi

java - Spring-Ws 从响应中删除 namespace 前缀(ns2,ns3 ...)

转载 作者:行者123 更新时间:2023-11-30 10:32:14 26 4
gpt4 key购买 nike

我知道这已被问过 10 万次。但我做不到。响应总是如下:

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

<ns2:getReportResponse xmlns:ns2="http://www.example.net/myresource">

<ns2:Report>JVBE...</ns2:Report>

<ns2:Messages/>

</ns2:getReportResponse>

</SOAP-ENV:Body>

我想得到:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

<getReportResponse xmlns="http://www.example.net/example">

<Report>JVBE...</Report>

<Messages/>

</getReportResponse>

</SOAP-ENV:Body>

我已经尝试设置 elementFormDefault="unqualified" 但没有任何成功(完成后我得到的响应是空的......)。有任何想法吗? @NameSpace(prefix=""namespace=NAMESPACE_URI) 也没有帮助...

提前致谢。

附言。我喜欢使用纯 Spring-WS 解决方案,而不是使用任何类型的 marshaller 等来自 jaxb

最佳答案

我设法自己修复了它。即使我没有从社区得到答案,我也想在这里写下答案,这样就没有人需要为我今天所拥有的东西而苦苦挣扎。

我不确定是否有一种方法可以直接使用神奇的 Spring @notation 来实现这一点。尽管如此,我自己还是设法做到了这一点:

我创建了一个 EndpointInterceptor,我在其中实现了一个后处理器以删除不需要的前缀。

public class GlobalEndpointInterceptor implements EndpointInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext, Object o) throws Exception {
return true;
}

@Override
public boolean handleResponse(MessageContext messageContext, Object o) throws Exception {
return true;
}

@Override
public boolean handleFault(MessageContext messageContext, Object o) throws Exception {
return false;
}

@Override
public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
try {
SOAPMessage soapMessage = ((SaajSoapMessage) messageContext.getResponse()).getSaajMessage();
SOAPHeader header = soapMessage.getSOAPHeader();
SOAPBody body = soapMessage.getSOAPBody();
header.setPrefix("");
body.setPrefix("");
Iterator<SOAPBodyElement> it = body.getChildElements();
while (it.hasNext()) {
SOAPBodyElement node = it.next();
node.setPrefix("");
Iterator<SOAPBodyElement> it2 = node.getChildElements();
while (it2.hasNext()) {
SOAPBodyElement node2 = it2.next();
node2.setPrefix("");
Iterator<SOAPBodyElement> it3 = node2.getChildElements();
while (it3.hasNext()) {
SOAPBodyElement node3 = it3.next();
node3.setPrefix("");
}
}
}
} catch (SOAPException exx) {
exx.printStackTrace();
}
}
}

然后简单地将实现的拦截器添加到WS配置中,如下:

    @Configuration
@EnableWs
public class ExampleConfiguration extends WsConfigurerAdapter {

@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
interceptors.add(new GlobalEndpointInterceptor());
}
.
.
.
}

关于java - Spring-Ws 从响应中删除 namespace 前缀(ns2,ns3 ...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42808187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com