Java 摆脱从 ant 调用的 wsimport 生成的类中的 JAXBElement
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2177153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get rid of JAXBElement in classes generated by wsimport called from ant
提问by MisterY
I have the following problem: I'm using the wsimport ant task to create a webservice client (for salesforce.com). Everything's working fine but the generated classes all use this strange JAXBElement class for all bean properties.
我有以下问题:我正在使用 wsimport ant 任务来创建网络服务客户端(用于 salesforce.com)。一切正常,但生成的类都使用这个奇怪的 JAXBElement 类来处理所有 bean 属性。
Eg:
例如:
public void setLastName(JAXBElement<String> value) { this.lastName = ((JAXBElement<String> ) value); } public JAXBElement<String> getCountry() { return country; }
public void setLastName(JAXBElement<String> value) { this.lastName = ((JAXBElement<String> ) value); } public JAXBElement<String> getCountry() { return country; }
Instead of wrapping all classes in JAXBElement I'd like to have simple methods like setLastName(String newLastName). That's how I'm calling the wsimport task.
与其将所有类都包装在 JAXBElement 中,我希望使用像 setLastName(String newLastName) 这样的简单方法。这就是我调用 wsimport 任务的方式。
<wsimport debug="false" verbose="false" keep="${keep}"
extension="${extension}" destdir="${tmp.metro}"
wsdl="${licensing.wsdl}"
sourcedestdir="${licensingws.generated.src}"
>
<binding dir="${basedir}/etc" includes="${client.binding}"
/>
<arg value="-B-XautoNameResolution" />
</wsimport>
The task is defined this way:
任务是这样定义的:
Does anybody know what I have to set so that wsimport generates the classes the way I want? Thanks a lot in advance!!!
有人知道我必须设置什么以便 wsimport 以我想要的方式生成类吗?非常感谢提前!!!
采纳答案by jarnbjo
The reason for using JAXBElement wrappers in the generated beans is probably, that the WSDL declares the field to be both optional and nillable. To distinguish between "not present" and "present, but null", the String type cannot be used directly, since the String in both cases would be null.
在生成的 bean 中使用 JAXBElement 包装器的原因可能是,WSDL 声明该字段既是可选的又是可空的。为了区分“不存在”和“存在,但为空”,不能直接使用 String 类型,因为这两种情况下的 String 都是空的。
If you don't need to distinguish between the two situations, you can configure the code generatorto use the String type instead. I'm not 100% sure how to do this with the ant task, but you'll probably find the relevant information in the ant task documentation.
如果不需要区分这两种情况,可以将代码生成器配置为使用 String 类型。我不是 100% 确定如何使用 ant 任务执行此操作,但您可能会在 ant 任务文档中找到相关信息。
回答by rodrigoap
You will have to customize the binding.
您将不得不自定义绑定。
回答by Laxman G
I have faced similar problem.
我遇到过类似的问题。
I have used below binding XML while ceiling wsimport :with reference of this link.
我在天花板 wsimport 时使用了以下绑定 XML:参考此链接。
Binding file
绑定文件
<jaxb:bindings version="2.1"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jaxb:globalBindings generateElementProperty="false"/>
</jaxb:bindings>
Sample wsimport command:
示例 wsimport 命令:
wsimport -keep <WSDL_location> -b employerServiceWSD
L_binding.xjb
Note :employerServiceWSDL_binding.xjb contain of above binding xml entry.
注意:雇主服务WSDL_binding.xjb 包含上述绑定 xml 条目。
Hope it will work for other also.
希望它也适用于其他人。
回答by mandrin
Resolution is to use 'Binding' file, and add converter for each data types. Below is my example, you can modify the javaType as per your WSDL. Also, tutorials point described methods how to pass from wsdl -> java POJO properly.
解决方案是使用“绑定”文件,并为每种数据类型添加转换器。下面是我的示例,您可以根据您的 WSDL 修改 javaType。此外,教程指出了如何正确地从 wsdl -> java POJO 传递的方法。
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="1.0"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<jaxb:globalBindings generateElementProperty="false">
<jaxb:serializable uid="1"/>
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
<jaxb:javaType name="java.util.Calendar" xmlType="xs:date"
parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
printMethod="javax.xml.bind.DatatypeConverter.printDate" />
<jaxb:javaType name="java.util.Calendar" xmlType="xs:time"
parseMethod="javax.xml.bind.DatatypeConverter.parseTime"
printMethod="javax.xml.bind.DatatypeConverter.printTime" />
</jaxb:globalBindings>
</jaxb:bindings>