錯誤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