gpt4 book ai didi

java - 如何在编码/解码期间忽略基类?

转载 作者:行者123 更新时间:2023-12-04 05:05:45 27 4
gpt4 key购买 nike

我有一个扩展 Thread 的类,我希望能够将配置信息存储为 xml。由于所有其他属性以及 Thread 中没有的内容,这显然不起作用。类。在编码期间,我得到了一个很好的堆栈跟踪:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 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 Test
java.lang.Thread$UncaughtExceptionHandler does not have a no-arg default constructor.
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 Test

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:451)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:283)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at Test.main(Test.java:22)

这是一个简单的类来演示这个问题:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;



@XmlRootElement
public class Test extends Thread{

@XmlAttribute
public String something;

public Test(){
}

public static void main(String[] args) throws JAXBException {
Test test = new Test();
test.something = "My Value";

JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(test, System.out);

}

}

基本上我只关心 Test 中的内容类而不是继承的东西。所以像:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test something="My Value"/>

那么如何在编码/解码期间忽略基类?

最佳答案

为什么要扩展Thread而不实现Runnable?

无论如何,我认为您的解决方案不是编码和解码 Thread 或任何扩展它的类,而是创建一个单独的 Configuration 类来保存您将编码和解码的重要状态数据。

例如,

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

public class Test implements Runnable {

private TestConfig config = new TestConfig();

public Test() {
}

public void setConfigSomething(String something) {
config.setSomething(something);
}

public TestConfig getConfig() {
return config;
}

public void setConfig(TestConfig config) {
this.config = config;
}

@Override
public void run() {
// foo
}

public static void main(String[] args) throws JAXBException {
Test test = new Test();
test.setConfigSomething("My Value");

JAXBContext jaxbContext = JAXBContext.newInstance(TestConfig.class);

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(test.getConfig(), System.out);
}
}

@XmlRootElement
class TestConfig {
private String something;

@XmlAttribute
public String getSomething() {
return something;
}

public void setSomething(String something) {
this.something = something;
}
}

这将导致预期的输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testConfig something="My Value"/>

我想另一种可能的解决方案是尝试使用诸如 XmlAdapter 之类的适配器类型,也许最好的解决方案是使用 MOXy as per Blaise Doughan's suggestion .

关于java - 如何在编码/解码期间忽略基类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514701/

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