gpt4 book ai didi

soap - JAXWS 中 SOAP 请求参数的 JAXB 绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 05:39:59 24 4
gpt4 key购买 nike

我的任务是为更新操作编写一个网络服务,其中将对象列表传递给方法。

@WebMethod(operationName = "updateObjects", action = "urn:preferences")
public boolean updateObjects(List<MyObject> objects){

}

MyObject 类非常简单。

 @XmlRootElement(name="Object")
public class MyObject{
private String item1;
private String item2;
}

现在是问题陈述。当我查看此方法的 SOAP 请求(为我生成的 SOAP UI)时,请求如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pref="preferences">
<soapenv:Header/>
<soapenv:Body>
<pref:updateObjects>
<!--Zero or more repetitions:-->
<arg0>
<!--Optional:-->
<item1>?</item1>
<!--Optional:-->
<item2>?</item2>
</arg0>
</pref:updateObjects>
</soapenv:Body>
</soapenv:Envelope>

但我希望它看起来像下面这样。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pref="preferences">
<soapenv:Header/>
<soapenv:Body>
<pref:updateObjects>
<!--Zero or more repetitions:-->
<Objects>
<Object>
<!--Optional:-->
<item1>?</item1>
<!--Optional:-->
<item2>?</item2>
</Object>
<Object>
<!--Optional:-->
<item1>?</item1>
<!--Optional:-->
<item2>?</item2>
</Object>
</Objects>
</pref:updateObjects>
</soapenv:Body>
</soapenv:Envelope>

有人可以请教。提前致谢。

最佳答案

您只需要为您的 List 添加一个“包装器”像这样的对象:

 @XmlRootElement(name="objects")
public class MyObjects{

@XmlElement(name="object")
List<MyObject> myObjects;
}

public class MyObject{
private String item1;
private String item2;
}

注意:从 arg0 更改根元素至 objects带有标签 @XmlRootElement(name="objects")不会工作,因为你的 <objects>不是 Web 服务定义中的根元素。实际上它是你的一部分 <wsdl:message> (因此 JAXB 将丢弃它)。

您需要更改的是添加 @WebParam(name = "objects") 的 Web 服务消息给你的@WebMethod喜欢:

@WebMethod(operationName = "updateObjects", action = "urn:preferences")
public boolean updateObjects(@WebParam(name = "objects") MyObjects objects){

}

如果您不想使用“包装器”,您可以保留 WebMethod但是像这样:

@WebMethod(operationName = "updateObjects", action = "urn:preferences")
public boolean updateObjects(@WebParam(name = "object") List<MyObject> objects){

}

但是你会丢失 <objects> wrapper 。请求应该是这样的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pref="preferences">
<soapenv:Header/>
<soapenv:Body>
<pref:updateObjects>
<!--Zero or more repetitions:-->
<object>
<!--Optional:-->
<item1>?</item1>
<!--Optional:-->
<item2>?</item2>
</object>
</pref:updateObjects>
</soapenv:Body>
</soapenv:Envelope>

关于soap - JAXWS 中 SOAP 请求参数的 JAXB 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11133401/

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