部署spring-ws Web服务时出现JAXB异常

时间:2013-02-26 16:35:23

标签: java exception jaxb spring-ws

我有一个xsd,用于使用spring-ws实现的soap web服务。 其中一种类型定义如下:

   <xsd:complexType name="DateTimeWithTimeZone">
      <xsd:annotation>
         <xsd:documentation>An extension to the standard dateTime, that forces the use of a proper time zone.</xsd:documentation>
      </xsd:annotation>
      <xsd:attribute name="DateTime" type="xsd:dateTime" use="required">
         <xsd:annotation>
            <xsd:documentation>The date time value</xsd:documentation>
         </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="TimeZone" type="xsd:string" use="optional" default="UTC">
         <xsd:annotation>
            <xsd:documentation>The time zone of the relevant date/time, should be a standard time zone from the Time Zone database at http://www.iana.org/time-zones</xsd:documentation>
         </xsd:annotation>
      </xsd:attribute>
   </xsd:complexType>

我的问题是,当我的JBoss加载时,我得到了这个例外:

ERROR [org.springframework.web.context.ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'marshaller' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.oxm.UncategorizedMappingException: Unknown JAXB exception; nested exception is com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
    this problem is related to the following location:
        at protected javax.xml.datatype.XMLGregorianCalendar com.example.schema.DateTimeWithTimeZone.dateTime
        at com.example.schema.DateTimeWithTimeZone
   ...

生成的java文件如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DateTimeWithTimeZone")
public class DateTimeWithTimeZone {

    @XmlAttribute(name = "DateTime", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar dateTime;
    @XmlAttribute(name = "TimeZone")
    protected String timeZone;

    public XMLGregorianCalendar getDateTime() {
        return dateTime;
    }
    public void setDateTime(XMLGregorianCalendar value) {
        this.dateTime = value;
    }
    public String getTimeZone() {
        if (timeZone == null) {
            return "UTC";
        } else {
            return timeZone;
        }
    }
    public void setTimeZone(String value) {
        this.timeZone = value;
    }
}

如何摆脱这种异常?

1 个答案:

答案 0 :(得分:2)

嗯,这看起来很奇怪。我找不到一个很好的理由为什么这不起作用。您是否尝试使用自定义绑定文件将xs:datetime转换为使用Java Calendar对象?似乎以某种方式打破了XmlGregorianCalendar。也许正常的Calendar会起作用。

该文件看起来像:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <globalBindings>
    <javaType name="java.util.Calendar" xmlType="xs:dateTime"
      parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
      printMethod="javax.xml.bind.DatatypeConverter.printDate"
    />
  </globalBindings>
</bindings>

您可以将其添加到您的pom.xml中,如下所示:

<configuration>
    <bindingFiles>/path/to/bindings.xjb</bindingFiles>
<configuration>

或者使用xjc:

xjc schema.xsd -b bindings.xjb