IllegalAnnotationExceptions包$Class的java xx计数是一个非静态内部类,JAXB无法处理这些
您好,我有Java Bean-like blow,我需要用它创建xml文件,但运行程序时出错,请说:
mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Template$Random is a non-static inner class, and JAXB can't handle those.
this problem is related to the following location:
at mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Template$Random
at public mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Template$Random mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Template.getRandom()
at mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Template
at public mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Template mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Category.getTemplate()
at mehritco.ir.megnatis.ejb.nlp.aiml.Aiml$Category
at public java.util.List mehritco.ir.megnatis.ejb.nlp.aiml.Aiml.getCategories()
at mehritco.ir.megnatis.ejb.nlp.aiml.Aiml
at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:471)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:303)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:139)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1156)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:165)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:288)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:277)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:400)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
JavaBean:
package mehritco.ir.megnatis.ejb.nlp.aiml;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "aiml")
public class Aiml {
List<Category> categories = new ArrayList<Category>();
public List<Category> getCategories() {
return categories;
}
@XmlElement
public void setCategories(List<Category> categories) {
this.categories = categories;
}
@XmlRootElement(name = "category")
public class Category<T>{
Pattern pattern ;
Template<T> template ;
public Pattern getPattern() {
return pattern;
}
@XmlElement
public void setPattern(Pattern pattern) {
this.pattern = pattern;
}
public Template getTemplate() {
return template;
}
@XmlElement
public void setTemplate(Template template) {
this.template = template;
}
}
@XmlRootElement(name = "pattern")
public class Pattern{
String pattern;
public String getPattern() {
return pattern;
}
@XmlElement
public void setPattern(String pattern) {
this.pattern = pattern;
}
}
@XmlRootElement(name = "template")
public class Template<T>{
Random random;
String template;
public Random getRandom() {
return random;
}
public void setRandom(Random random) {
this.random = random;
}
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
@XmlRootElement(name = "random")
public class Random {
List<String> li = new ArrayList<String>();
public List<String> getLi() {
return li;
}
@XmlElement(name = "li")
public void setLi(List<String> li) {
this.li = li;
}
}
}
}
主要方法是:
Aiml aiml = new Aiml();
List<Aiml.Category> listCategory = new ArrayList<Aiml.Category>();
Aiml.Category category = aiml.new Category();
Aiml.Pattern pattern = aiml.new Pattern();
pattern.setPattern("Hello");
category.setPattern(pattern);
Aiml.Template<Aiml.Template.Random> template = aiml.new Template<Aiml.Template.Random>();
Aiml.Template.Random random = template.new Random();
List<String> list = random.getLi();
list.add("hi");
list.add("hello");
random.setLi(list);
template.setRandom(random);
category.setTemplate(template);
listCategory.add(category);
aiml.setCategories(listCategory);
try {
File file = new File("D:\\TestFile\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Aiml.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(aiml, file);
jaxbMarshaller.marshal(aiml, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
我需要创建java类来创建aiml对象,以便保存,读取我创建的对象,但在创建的这个类型类中无法完成,使用JAXB有什么问题? the xml format I'm need to create is here以及其他一些可能发生的情况,如:
<category>
<pattern>ONCE I *</pattern>
<template>
<random>
<li>Go on.</li>
<li>Can you be more specific?</li>
<li>I did not know that.</li>
<li>Are you telling the truth?</li>
<li>I don't know what that means.</li>
<li>Try to tell me that another way.</li>
<li>What is it?</li>
</random>
</template>
</category>
现在我需要知道这里有什么例外,必须做什么
共 (0) 个答案