gpt4 book ai didi

java - 如何在发送请求时强制 JAXB 写入 xsi :type of a javax. jws.WebParam(CXF 实现)

转载 作者:行者123 更新时间:2023-12-01 17:24:40 26 4
gpt4 key购买 nike

我在 wsdl 上使用 wsdl2java 生成了类,服务接口(interface)如下所示

@WebService(targetNamespace = "http://www.sii.example.it/SWG1", name = "SWG1")
@XmlSeeAlso({it.au.switchgas.swg1.messaggio.ObjectFactory.class, it.au.switchgas.swg1.strutturegenerali.ObjectFactory.class, it.au.switchgas.strutturegas.ObjectFactory.class, it.au.switchgas.swg1.flussi.ObjectFactory.class})
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface SWG1 {

@WebMethod(operationName = "SWG1.0050", action = "SWG1.0050")
@WebResult(name = "MessaggioSII", targetNamespace = "http://www.sii.example.it/SWG1", partName = "MessaggioSII")
public it.au.switchgas.swg1.messaggio.AmmissibilitaRichiestaMessaggioSIIType swg10050(
@WebParam(partName = "MessaggioSII", name = "MessaggioSII")
it.au.switchgas.swg1.messaggio.RichiestaSwitchingMessaggioSIIType messaggioSII
);
}

生成的请求是这样的

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:SWG1.0050 xmlns:ns1="http://www.sii.example.it/SWG1">
<MessaggioSII
xmlns:ns5="http://www.example.it/schemas/2010/SII_AU/MessaggioSII">
<ns5:RichiestaSII xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:RichiestaSwitchingRichiestaSIIType">
<ns5:AzioneRichiesta>
<ns5:Servizio>XYZ</ns5:Servizio>
<ns5:Operazione>ABC</ns5:Operazione>
</ns5:AzioneRichiesta>
<ns5:Erogatore>AXX</ns5:Erogatore>
<ns5:Fruitore>UXXXXX</ns5:Fruitore>
</ns5:RichiestaSII>
<ns5:DatiSII xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:RichiestaSwitchingDatiSIIType">
</ns5:DatiSII>
</MessaggioSII>
</ns1:SWG1.0050>
</soap:Body>
</soap:Envelope>

服务器(我无法以任何方式修改)拒绝提示 MessaggioSII 元素缺少 xsi:type 的请求,并需要此语法

<MessaggioSII xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:RichiestaSwitchingDatiSIIType"
xmlns:ns5="http://www.example.it/schemas/2010/SII_AU/MessaggioSII">

有没有办法强制CXF/JAXB写入xsi:type属性?我看过cxf documentation对于转换,遗憾的是它不是很清楚。我不想修改生成的类,但我对此持开放态度。

最佳答案

作为解决方法,我添加了此属性

@XmlAttribute(name = "type", namespace = XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
private String xsiType = "ns5:RichiestaSwitchingMessaggioSIIType";

它确实有效,但它特别脆弱,因为它依赖于我在构建时知道将生成的 namespace 别名。

更好的方法是修改出站拦截器中的 SOAP 体,请参阅How To Modify The Raw XML message of an Outbound CXF Request?howto modify a webservice request using CXF interceptors with org.w3c.dom.Node

顺便说一句,向服务添加注释@SchemaValidation(type = SchemaValidationType.OUT)似乎会导致客户端自动添加xsi:type信息。 WTF。

关于java - 如何在发送请求时强制 JAXB 写入 xsi :type of a javax. jws.WebParam(CXF 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61226278/

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