错误500不是真正的错误。看看你的日志,你很可能会看到真正的错误。
这里是一个MCVE(最小的,完整的和可验证的例子):
import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlRootElement;
public class Test {
public static void main(String[] args) throws Exception {
JAXBContext.newInstance(Activity.class);
}
}
@XmlRootElement
class Activity {
public String description;
public int duration;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
运行它给这个错误:
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "description"
this problem is related to the following location:
at public java.lang.String Activity.getDescription()
at Activity
this problem is related to the following location:
at public java.lang.String Activity.description
at Activity
Class has two properties of the same name "duration"
this problem is related to the following location:
at public int Activity.getDuration()
at Activity
this problem is related to the following location:
at public int Activity.duration
at Activity
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:498)
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 Test.main(Test.java:7)
正如你看到的,让现场public
意味着JAXB看到两个同名的属性,一个由public
字段定义,另一个由Bean方法定义。
由于如果将字段public
设置为getter和setter方法没有任何意义,如果您希望字段为public
,请删除方法。我建议不要让他们public
。你为什么?当你有getter和setter方法来访问字段时,并不需要。
如果您将该字段设置为公开,那么获取和设置方法有什么意义?当你公开时,系统会看到两个同名的值,即字段和属性(get/set)。删除get/set方法,或保持字段不公开。 – Andreas