0
我正在尋找使用JAXBContext將單例對象保存到XML文件。所討論的Object被稱爲Player,它正在擴展抽象類Entity。實體也擴展了Thread。由於Player是一個Singleton對象,因此它的構造函數是私有的,並且不能從類外部訪問。 我該如何正確地做到這一點?如何使用JAXBContext將單例對象保存到XML文件?
下面是一段代碼,我用它來保存播放器:
public static void savePlayer() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Player.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
OutputStream os = null;
try {
os = new FileOutputStream(StringConstants.PLAYER_SAVE_FILE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
m.marshal(Player.getInstance(), os);
}
而且這裏的一段代碼來再讀一遍:
public static Player readPlayer() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Player.class);
Unmarshaller u = context.createUnmarshaller();
File f = new File(StringConstants.PLAYER_SAVE_FILE);
return (Player) u.unmarshal(f);
}
在這兩個類別的實體和球員,我使用以下加註:
@XmlRootElement
Richt以上類的開始。
以下是錯誤輸出,我得到:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.lang.Thread$UncaughtExceptionHandler is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.lang.Thread$UncaughtExceptionHandler
at public java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getUncaughtExceptionHandler()
at java.lang.Thread
at model.entities.Entity
at model.entities.Player
at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at model.io.SaveWriter.savePlayer(SaveWriter.java:20)
at tests.SaveTest.main(SaveTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.lang.Thread$UncaughtExceptionHandler is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.lang.Thread$UncaughtExceptionHandler
at public java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getUncaughtExceptionHandler()
at java.lang.Thread
at model.entities.Entity
at model.entities.Player
at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at model.io.SaveReader.readPlayer(SaveReader.java:18)
at tests.SaveTest.main(SaveTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
您是否在Player類設置器中使用@XmlElement註釋?你需要標記你想要的東西。你可以在本教程中找到更多內容:http://www.java2blog.com/2013/02/jaxb-tutorial.html – RichardK 2015-02-23 14:54:16
到目前爲止,我沒有像Player類目前沒有setter(除了Entity中的那些它延伸的類)。我認爲這個問題是JAXBContext的實例化。 Player和Entity類都沒有公共構造函數。我嘗試應用您提到的註釋,但這並沒有改變錯誤日誌中的任何內容。 – Thibstars 2015-02-23 15:56:39