2012-03-07 69 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; 
    } 

} 

更多信息