java.lang.NoClassDefFoundError: org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl$Parser
When you encounter the following error that related to web service. java.lang.NoClassDefFoundError: org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl$Parser
Solution 1:
Ensure no xercesImpl.jar in your classpath since the implementation of xerce is built-in wiht JAVA SE 1.6.
Solution 2:
Use XmlJavaTypeAdapter [ @XmlJavaTypeAdapter(JaxbDateAdapter.class)] to convert XMLGregorianCalendar type to Date type. XMLGregorianCalendar is the built-in Java data type for JAXB mapping with datetime.
Example:
Binding.xjb
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<globalBindings>
<javaType name="java.util.Date" xmlType="xsd:dateTime"
parseMethod="DateConverter.parseDate"
printMethod="DateConverter.printDate" />
<serializable uid="100" />
</globalBindings>
</bindings>
JaxbDateAdapter.java:
public class JaxbDateAdapte extends XmlAdapter
{
public Date unmarshal(String value) {
return (JaxbConverter .parseDate(value));
}
public String marshal(Date value) {
return (JaxbConverter .printDate(value));
}
}
JaxbConverter .java:
public class JaxbConverter {
public JaxbConverter() {
}
public static Date parseDate(String value){
XMLGregorianCalendar xmlCalendar = null;
Date date = null;
try {
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(value);
GregorianCalendar calendar = xmlCalendar.toGregorianCalendar();
date = calendar.getTime();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
date = new Date();
}
return date;
}
public static String printDate(Date value){
XMLGregorianCalendar xmlCalendar = null;
GregorianCalendar calendar = null;
try {
calendar = new GregorianCalendar();
calendar.setTimeInMillis(value.getTime());
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
return xmlCalendar.toXMLFormat();
}
}
Jaxb Class:
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
@XmlSchemaType(name = "dateTime")
protected Date scheduledDatetime;
Solution 1:
Ensure no xercesImpl.jar in your classpath since the implementation of xerce is built-in wiht JAVA SE 1.6.
Solution 2:
Use XmlJavaTypeAdapter [ @XmlJavaTypeAdapter(JaxbDateAdapter.class)] to convert XMLGregorianCalendar type to Date type. XMLGregorianCalendar is the built-in Java data type for JAXB mapping with datetime.
Example:
Binding.xjb
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<globalBindings>
<javaType name="java.util.Date" xmlType="xsd:dateTime"
parseMethod="DateConverter.parseDate"
printMethod="DateConverter.printDate" />
<serializable uid="100" />
</globalBindings>
</bindings>
JaxbDateAdapter.java:
public class JaxbDateAdapte extends XmlAdapter
{
public Date unmarshal(String value) {
return (JaxbConverter .parseDate(value));
}
public String marshal(Date value) {
return (JaxbConverter .printDate(value));
}
}
JaxbConverter .java:
public class JaxbConverter {
public JaxbConverter() {
}
public static Date parseDate(String value){
XMLGregorianCalendar xmlCalendar = null;
Date date = null;
try {
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(value);
GregorianCalendar calendar = xmlCalendar.toGregorianCalendar();
date = calendar.getTime();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
date = new Date();
}
return date;
}
public static String printDate(Date value){
XMLGregorianCalendar xmlCalendar = null;
GregorianCalendar calendar = null;
try {
calendar = new GregorianCalendar();
calendar.setTimeInMillis(value.getTime());
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
return xmlCalendar.toXMLFormat();
}
}
Jaxb Class:
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(JaxbDateAdapter.class)
@XmlSchemaType(name = "dateTime")
protected Date scheduledDatetime;
留言
張貼留言