gpt4 book ai didi

java - JAXB异常 : SaveButton nor any of its super class is known to this context

转载 作者:行者123 更新时间:2023-12-01 18:11:44 26 4
gpt4 key购买 nike

我已经阅读了有关此主题的多个主题,但似乎没有一个能回答我的具体问题。在包“settings”中,我有一个 SettingModel.java 类,它使用 jaxb 从 default.xml 文件加载默认设置。这工作完美无缺。然后,在同一个包下,在名为 SettingsWidgets 的子包内,我有一个 SaveButton.java 类,它有一个 setOnMouseClick eventListener,触发方法“saveSettings”。该代码部分工作:当前文件被覆盖(尽管为空)或在所需的包中创建了新的(但为空)xml 文件,并且我收到错误

javax.xml.bind.JAXBException: class settings.settingsWidgets.SaveButton nor any of its super class is known to this context.
<小时/>

我尝试过:
按照 Stack O 上几个线程中建议的不同方式创建 JAXBContext (将包作为字符串传递并添加 jaxb.in​​dex 和 ObjectFactory.java ,传递完整的类路径而不仅仅是类,...)无济于事,在我看来(阅读错误)问题不在于我如何将 SettingsModel 类传递给上下文...
@XmlSeealso 解决方法也经常被提及,但在我的例子中我没有立即看到它有任何用途(这并不是说我试图编码(marshal)子类或某种东西)
还尝试将 SaveButton.java 从子包中重构出来,使其与 SettingsModel 处于同一级别,但这并没有改变任何内容...
我完全不知道如何以及为什么应该将 SaveButton 包含到 jaxbContext 中,当我尝试时,我收到很多错误。

<小时/>

我的代码:

// SettingsModel.java

@XmlRootElement(name = "robot")
@XmlAccessorType(XmlAccessType.FIELD)
public class SettingsModel implements Observable {

private boolean valid = true;

//registered views List
//annotate as Type Object.Class because JAXB cannot handle interfaces
@XmlElement(type = Object.class)
private ArrayList<InvalidationListener> listenersList;

//list all variable robot/vehicle specs
//filename is needed for saving to current or new file
@XmlElement(name = "filename")
private String fileName;

@XmlElement(name = "vehicle-width")
private double vehicleWidth;

@XmlElement(name = "work-width")
private double workWidth;

// other getters and setters without logic are removed

public void setValid(boolean valid) {
if (valid != this.valid) {
this.valid = valid;

fireInvalidationEvent();
}
}

public void setVehicleWidth(double vehicleWidth) {
this.vehicleWidth = vehicleWidth;

Field.getInstance().getRobot().setVehicleWidth(vehicleWidth);
fireInvalidationEvent();
}

public void setWorkWidth(double workWidth) {
this.workWidth = workWidth;

// Field.getInstance().getRobot().setVehicleWidth(vehicleWidth);
// fireInvalidationEvent();
}



//METHODS AND FUNCTIONS
@Override
public void addListener(InvalidationListener invalidationListener) {
listenersList.add(invalidationListener);
}

@Override
public void removeListener(InvalidationListener invalidationListener) {
listenersList.remove(invalidationListener);
}

public void fireInvalidationEvent() {
for (InvalidationListener listener : listenersList) {
listener.invalidated(this);
}
}
}
// SaveButton.java

public class SaveButton extends Button implements InvalidationListener {

private SettingsModel model;

public SaveButton(SettingsModel model) {
this.model = model;
model.addListener(this);
setText("SAVE SETTINGS");
setOnMouseClicked((ev)->{
try {
saveSettings();
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
});
}

private void saveSettings() throws JAXBException, FileNotFoundException {
String filename = model.getFileName();

Object[] options = {"Save", "Save As"};

//choose Save or Save As
int choice = JOptionPane.showOptionDialog(null,"Overwrite current file or Save As New file?",
"Save Settings",
JOptionPane.WARNING_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]
);

if(choice != JOptionPane.OK_OPTION){
filename = JOptionPane.showInputDialog("Enter new file name");
model.setFileName(filename);
}

System.out.println(model.getFileName());

//Marshal
JAXBContext jaxbContext = JAXBContext.newInstance(SettingsModel.class);

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

//if filename stays same, file is overwritten. Test only SAVE AS until code works to not lose default file
File file = new File("src/resources/properties/"+ filename + ".xml");

OutputStream os = new FileOutputStream(file);

jaxbMarshaller.marshal(model,os);
}

@Override
public void invalidated(Observable observable) {
System.out.println("button invalidated: "+ model.isValid());

setDisable(!model.isValid());
}
}
<小时/>

堆栈跟踪:

javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: javax.xml.bind.JAXBException: class settings.settingsWidgets.SaveButton nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class settings.settingsWidgets.SaveButton nor any of its super class is known to this context.]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:301)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:226)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:80)
at settings.settingsWidgets.SaveButton.saveSettings(SaveButton.java:59)
at settings.settingsWidgets.SaveButton.lambda$new$0(SaveButton.java:27)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: com.sun.istack.SAXException2: javax.xml.bind.JAXBException: class settings.settingsWidgets.SaveButton nor any of its super class is known to this context.
javax.xml.bind.JAXBException: class settings.settingsWidgets.SaveButton nor any of its super class is known to this context.
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:217)
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:232)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:623)
at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:39)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:142)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:129)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:329)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:563)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:310)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:464)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:298)
... 34 more
Caused by: javax.xml.bind.JAXBException: class settings.settingsWidgets.SaveButton nor any of its super class is known to this context.
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:563)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:618)
... 42 more

Process finished with exit code 0

任何见解将不胜感激。

最佳答案

您的 SettingsModel 有一个标记为 xml 元素的监听器列表。您可以使用以下行将 SaveButton 添加到列表中: model.addListener(this);

因此,在编码 SettingsModel 时,JAXB 处理 ListenerList 并尝试编码 SaveButton,但它不知道如何执行此操作。

向 SaveButton 类添加 @XmlRootElement 注释就足够了。

此外,我看到每次单击按钮时都会创建一个 JAXBContext。 JAXB 规范说:

To avoid the overhead involved in creating a JAXBContext instance, a JAXB application is encouraged to reuse a JAXBContext instance. An implementation of abstract class JAXBContext is required to be thread-safe, thus, multiple threads in an application can share the same JAXBContext instance.

您应该将 JAXBContext 移至静态字段,因为该实例可以重用。

关于java - JAXB异常 : SaveButton nor any of its super class is known to this context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60457044/

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