Arc*_*rci 4 java wsdl axis2 jax-ws wsimport
我有一个来自Axis2 Web服务的WSDL文件.当我使用wsimport给定WSDL文件生成客户端存根时,生成的类需要JAXBElement参数.为什么会那样?
来自其中一个生成类的示例方法:
JAXBElement<DataBean> value;
public void setValue(JAXBElement<DataBean> value)
{
this.value = ((JAXBElement<DataBean>) value);
}
Run Code Online (Sandbox Code Playgroud)
我希望它看起来像这样(没有JAXBElement):
DataBean value;
public void setValue(DataBean value)
{
this.value= (DataBean) value;
}
Run Code Online (Sandbox Code Playgroud)
我在网上看到的教程没有将类设置为JAXBElement.可能是什么问题呢?请注意,服务器是Axis2 Web服务,WSDL文件由Axis2自动生成.假设是我无法控制服务器.
如何以wsimport不将参数转换为JAXBElements 的方式创建它?
以下是WSDL文件的摘录:(我忽略了一些标签只包含必要的标签)
<xs:element name="getData">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="getData" nillable="true" type="ax220:getData"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="getData">
<xs:sequence>
<xs:element minOccurs="0" name="value" nillable="true" type="ax219:DataBean"/>
</xs:sequence>
</xs:complexType>
<wsdl:message name="getDataRequest">
<wsdl:part name="parameters" element="ns:getData"/>
</wsdl:message>
<wsdl:message name="getDataResponse">
<wsdl:part name="parameters" element="ns:getDataResponse"/>
</wsdl:message>
<wsdl:operation name="getData">
<wsdl:input message="ns:getDataRequest" wsaw:Action="urn:getData"/>
<wsdl:output message="ns:getDataResponse" wsaw:Action="urn:getDataResponse"/>
</wsdl:operation>
<wsdl:operation name="getData">
<soap:operation soapAction="urn:getData" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getData">
<soap12:operation soapAction="urn:getData" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getData">
<http:operation location="getData"/>
<wsdl:input>
<mime:content type="text/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
Run Code Online (Sandbox Code Playgroud)
小智 6
正如本页所述:
http://www.techdevtips.com/java/java-webservice-client-how-to-remove-jaxbelement
使用此代码的数据绑定文件:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings generateElementProperty="false">
<xjc:simple />
</jaxb:globalBindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)
并通过填充绑定属性(或-b标志参数,如果您使用runnable)在wsimport ant任务中使用它
干杯:)
首先:我认为这是不可能做到的。也就是说,我认为您不能告诉 wsimport 以不同的方式生成类。不过,我可以告诉您如何以不同的方式修改 WSDL,以不同的方式生成模式,并且仍然可以使您与服务对话。
我从 WSDL 中获取了类型定义,调整了 complexType 的名称,并添加了上面缺少的 DataBean 的类型。我将其粘贴到模式中,并使用 xjc(JAXB 模式编译器)编译该模式,wsimport 使用 xjc 从类型定义生成类。这是架构:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/DataBean"
xmlns:tns="http://www.example.org/DataBean" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="getData">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="getDataType" type="tns:getDataType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="getDataType">
<xs:sequence>
<xs:element minOccurs="0" name="value" type="tns:DataBean" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="DataBean">
<xs:simpleContent>
<xs:extension base="xs:int" />
</xs:simpleContent>
</xs:complexType>
</schema>
Run Code Online (Sandbox Code Playgroud)
编译器不需要任何特殊选项,只需执行 xjc 并将其指向模式文件,然后它就会编译源文件。
生成的类不用作JAXBElement方法参数。那就是它们看起来像这样:
protected DataBean value;
public DataBean getValue() {
return value;
}
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我nillable="true"从元素定义中删除了属性,这样就成功了。nillable="true"声明显式空值在这里是合法的:
<DataBean></DataBean>
Run Code Online (Sandbox Code Playgroud)
如果删除此属性,如果服务实际上在其中写入空值,您的代码将会遇到问题。但毕竟 WSDL 已生成,也许 Axis2 只是认为由于某种原因 nillable 应该在那里,尽管实现从未实际使用过它。如果您幸运的话,虽然您修改了 WSDL,但事实并非如此,一切都会正常工作。
我希望这有帮助!如果没有,那么至少我今天学到了一些东西;-)
| 归档时间: |
|
| 查看次数: |
5673 次 |
| 最近记录: |