gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:25:54 27 4
gpt4 key购买 nike

我有一个 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;
}
}

我怎样才能摆脱这个异常?

最佳答案

嗯,这看起来很奇怪。我找不到这不起作用的充分理由。您是否尝试过使用自定义绑定(bind)文件将 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

关于java - 部署 spring-ws Web 服务时出现 JAXB 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15094401/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com