2013-10-03 72 views
1

我得到的異常的屬性或字段規範註釋是短暫的,從而不能包含在proporder註釋

的屬性或字段規範註釋是短暫這樣 不能被列入proporder註釋。

當試圖爲我正在使用的bean指定proporder時。我暫時沒有註釋,所以我不確定爲什麼這個異常在運行時會出現醜陋的一面。如果我刪除了proporder,我沒有得到例外,但是當打印XML時,我失去了「規範」字段,並且順序顯然是錯誤的。

我失蹤的任何想法?

這是我的工作類:

EndpointBean

@XmlRootElement(name = "endpoint") 
@XmlType (propOrder={"specification", "url", "changeset", "type", "formats"}) 
public class EndpointBean { 

    private String specification; 
    private String url; 
    private String changeset; 
    private String type; 
    @XmlElementWrapper(name = "formats") 
    @XmlElement(name = "format") 
    private ArrayList<String> formats; 

    public String getSpecification() { 
     return specification; 
    } 

    public void setSpefification(String spefification) { 
     this.specification = spefification; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public String getChangeset() { 
     return changeset; 
    } 

    public void setChangeset(String changeset) { 
     this.changeset = changeset; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public ArrayList<String> getFormats() { 
     return formats; 
    } 

    public void setFormats(ArrayList<String> format) { 
     this.formats = format; 
    } 

} 

堆棧跟蹤

Exception in thread "main" javax.xml.bind.JAXBException: 
Exception Description: The property or field specification is annotated to be transient so can not be included in the proporder annotation. 
- with linked exception: 
[Exception [EclipseLink-50009] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JAXBException 
Exception Description: The property or field specification is annotated to be transient so can not be included in the proporder annotation.] 
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1021) 
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
    at javax.xml.bind.ContextFinder.find(Unknown Source) 
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
    at conversion.Test.main(Test.java:12) 
Caused by: Exception [EclipseLink-50009] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JAXBException 
Exception Description: The property or field specification is annotated to be transient so can not be included in the proporder annotation. 
    at org.eclipse.persistence.exceptions.JAXBException.transientInProporder(JAXBException.java:229) 
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:879) 
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:282) 
    at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:150) 
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1017) 
    ... 15 more 

回答

2

,因爲你有一對get/set您收到此異常不匹配。由於在您發佈的代碼中沒有setSpecification方法,因此該屬性在內部處理,就好像它使用@XmlTransient映射一樣。

什麼你目前有:

public void setSpefification(String spefification) { 
    this.specification = spefification; 
} 

您需要更改設定器,以配合吸氣什麼:

public void setSpecification(String spefification) { 
    this.specification = spefification; 
} 
+1

布萊斯,你是在人間神。簡直不敢相信我錯過了一個公然的錯字。感謝那個例外的解釋,但是我不知道在那種情況下它會把這個財產視爲暫時的。 – Prmths

+1

@Prmths - 我很高興能幫上忙。 JAXB會忽略沒有匹配setter的getter,如果你使用'@ XmlElement'註解一個只有get屬性的屬性,那麼當你將對象編組爲XML時,它將被包含。 –