我有一个包含-的WSDL文件
<xs:complexType name="PNV">
<xs:sequence>
<xs:element name="dataType" type="xsd:int" />
<xs:element name="multiValued" type="xsd:boolean" />
<xs:element name="booleanValue" type="xsd:boolean" />
<xs:element name="dateValue" nillable="true" type="xsd:dateTime" />
..........
然后它被转换成这样的Java文件-
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PNV", propOrder = {
"dataType",
"multiValued",
"booleanValue",
"dateValue",
})
public class PNV
implements Serializable
{
private final static long serialVersionUID = 1L;
protected int dataType;
protected boolean multiValued;
protected boolean booleanValue;
@XmlElement(required = true, type = String.class, nillable = true)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected Date dateValue;
}
绑定文件-
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://changeservice.xxservices.service.xx']">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Date" xmlType="xs:dateTime"
parseMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDateTime"
printMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.printDateTime"/>
<jxb:serializable uid="1"/>
</jxb:globalBindings>
</jaxws:bindings>
在我的SOAPUI测试案例中,我通过了
<dateValue nil="true"/>
但它给了我以下错误-
Caused by: java.lang.IllegalArgumentException:
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parseAndSetYear(XMLGregorianCalendarImpl.java:2891) ~[na:1.8.0_201]
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl$Parser.parse(XMLGregorianCalendarImpl.java:2777) ~[na:1.8.0_201]
at com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.<init>(XMLGregorianCalendarImpl.java:436) ~[na:1.8.0_201]
at com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl.newXMLGregorianCalendar(DatatypeFactoryImpl.java:536) ~[na:1.8.0_201]
at javax.xml.bind.DatatypeConverterImpl._parseDateTime(DatatypeConverterImpl.java:422) ~[na:1.8.0_201]
at javax.xml.bind.DatatypeConverterImpl.parseDateTime(DatatypeConverterImpl.java:417) ~[na:1.8.0_201]
at javax.xml.bind.DatatypeConverter.parseDateTime(DatatypeConverter.java:327) ~[na:1.8.0_201]
at org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDateTime(DataTypeAdapter.java:65) ~[cxf-xjc-runtime-3.3.1.jar:na]
at org.w3._2001.xmlschema.Adapter1.unmarshal(Adapter1.java:13) ~[classes/:na]
at org.w3._2001.xmlschema.Adapter1.unmarshal(Adapter1.java:1) ~[classes/:na]
at com.sun.xml.bind.v2.runtime.reflect.AdaptedAccessor.set(AdaptedAccessor.java:55) ~[jaxb-runtime-2.3.3.jar:2.3.3]
... 107 common frames omitted
我检查了Adapter1类中的date属性,它是空的,而不是null,所以它抛出了一个错误。我的问题是,为什么即使我在SOAPUI测试用例中传递nil=true,它也不取NULL值。为什么它是空白的?我还试图从我的输入请求中完全删除这个dateValue字段,但它仍然给了我相同的日期字段错误。
我还尝试使用XMLGregorianCalendar作为日期值的类型,但当我在SOAPUI中传递nil=true时,它会给我同样的错误消息。。请帮忙。
这个链接帮助了我:https://blogs.oracle.com/rammenon/specifying-null-content-in-your-xml-document-using-nillable-and-xsi:nil
我添加了xmlns:xsi=";http://www.w3.org/2001/XMLSchema-instance在我的SOAPUI测试&添加日期值作为
<dateValue xsi:nil="true"/>
如果您无法更改wsdl文件(在由外部系统提供的情况下(,您可以覆盖标准org.apache.cxf.xjc.runtime.DataTypeAdapter
问题的根不是null,而是空字符串,作为parseDateTime方法的输入参数。
-
创建类似于";org.apache.cxf.xjc.runtime.DataTypeAdapter";对空字符串进行额外检查
public class DateAdapter { public static Date parseDateTime(String s) { if (s == null||s.isEmpty()) { return null; } return DatatypeConverter.parseDateTime(s).getTime(); } public static String printDateTime(Date dt) { if (dt == null) { return null; } Calendar c = Calendar.getInstance(); c.setTime(dt); return DatatypeConverter.printDateTime(c); }
}
-
在绑定文件中指定新类
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://changeservice.xxservices.service.xx']">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Date" xmlType="xs:dateTime"
parseMethod="your_class_here.parseDateTime"
printMethod="your_class_here.printDateTime"/>
<jxb:serializable uid="1"/>
</jxb:globalBindings>
</jaxws:bindings>