2012-03-07 74 views
3

我有一个声明Joda时间DateTime字段的类。JAXB为@XmlTransient字段抛出零参数构造函数错误

但是,这个值不是由unmarhsalling进程设置的,而是稍后在afterUnmarhsal方法中设置的。

因此,本场被标记为XmlTransient

@XmlTransient 
private DateTime startTime; 

但是,试图启动的时候,我碰到这样的错误:

javax.xml.bind.JAXBException: Exception Description: The class org.joda.time.DateTime$Property requires a zero argument constructor or a specified factory method. Note that non-static inner classes do not have zero argument constructors and are not supported. - with linked exception: [Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.2.0.v20110202-r8913) ...

为什么要JAXB关心这个类,考虑到这是明显的短暂的反编组& unmarshalling的目的?

如何让JAXB忽略该字段?我知道我可以在那里放置一个工厂方法,但它似乎没有意义,因为工厂将无法实例化该值(这就是为什么它在afterUnmarshal中完成)

+1

这个错误听起来很熟悉(我会调查)。你有没有尝试过一个更新的版本,目前的版本是2.3.2:http://www.eclipse.org/eclipselink/downloads/ – 2012-03-07 23:33:04

+0

@BlaiseDoughan谢谢你的跟进,以及最新版本的headup。刚试过2.3.2('2.3.2.v20111125-r10461'),并得到相同的错误。 FYI - [The Maven Page](http://wiki.eclipse.org/EclipseLink/Maven#Available_Released_Versions)已过期,仅列出了2.2.0版本(这是我错过的版本)。 – 2012-03-08 05:03:00

+0

我发布了一个答案(http://stackoverflow.com/a/9619140/383861)并更新了wiki页面(http://wiki.eclipse.org/EclipseLink/Maven#Available_Released_Versions)。 – 2012-03-08 14:31:21

回答

1

似乎有一个EclipseLink 2.2.0中的错误已在以后的EclipseLink版本中修复。你仍然会看到这个例外在最新的EclipseLink版本,如果你使用默认的访问(XmlAccessType.PUBLIC),但注释字段:

package forum9610190; 

import javax.xml.bind.annotation.XmlTransient; 
import org.joda.time.DateTime; 

public class Root { 

    @XmlTransient 
    private DateTime startTime; 

    public DateTime getStartTime() { 
     return startTime; 
    } 

    public void setStartTime(DateTime startTime) { 
     this.startTime = startTime; 
    } 

} 

选择1 - 注释属性

取而代之的注释字段,你可以用@XmlTransient注释属性(get方法):

package forum9610190; 

import javax.xml.bind.annotation.XmlTransient; 
import org.joda.time.DateTime; 

public class Root { 

    private DateTime startTime; 

    @XmlTransient 
    public DateTime getStartTime() { 
     return startTime; 
    } 

    public void setStartTime(DateTime startTime) { 
     this.startTime = startTime; 
    } 

} 

选项#2 - 注释字段,并使用@XmlAccessorType(XmlAcce ssType.FIELD)

如果你要注释字段,那么你将需要指定类@XmlAccessorType(XmlAccessType.FIELD)

package forum9610190; 

import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlTransient; 
import org.joda.time.DateTime; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Root { 

    @XmlTransient 
    private DateTime startTime; 

    public DateTime getStartTime() { 
     return startTime; 
    } 

    public void setStartTime(DateTime startTime) { 
     this.startTime = startTime; 
    } 

} 

更多信息