Java 解组从成功封送的XML返回空对象
我有以下作为XML编组的类Java 解组从成功封送的XML返回空对象,java,jaxb,marshalling,unmarshalling,Java,Jaxb,Marshalling,Unmarshalling,我有以下作为XML编组的类 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <customer id="100"> <age>21</age> <hobbies> <hobby> <cost>sd</cost> <name>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100">
<age>21</age>
<hobbies>
<hobby>
<cost>sd</cost>
<name>na</name>
</hobby>
<hobby>
<cost>sd</cost>
<name>nb</name>
</hobby>
</hobbies>
<name>test</name>
</customer>
21
sd
na
sd
铌
测试
但是,当我试图解组时,我只能创建customer对象,而不能创建hobby,它返回null
我做错什么了吗?问题似乎出在XML层次结构上
package com.mytest.jxb;
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.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement(name="customer")
@XmlSeeAlso({Hobby.class})
public class Customer {
String name;
int age;
int id;
@XmlElementRef
private List<Hobby> hobbyList = new ArrayList<Hobby>();
public Customer(){
//hobbyList = new ArrayList<Hobby>();
}
//@XmlElement
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
//@XmlElement
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
//@XmlAttribute
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
public void addHobby(Hobby h) {
hobbyList.add(h);
}
@XmlElementWrapper(name="hobbies")
@XmlElement(name = "hobby")
public List<Hobby> getHobbies(){
return hobbyList;
}
}
package com.mytest.jxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
class Hobby {
private String name;
private String cost;
public Hobby(){
}
public Hobby(String name, String cost){
this.name = name;
this.cost = cost;
}
//@XmlElement
public void setName(){
this.name = name;
}
@XmlElement
public String getName(){
return name;
}
//@XmlElement
public void setCost(){
this.cost = cost;
}
@XmlElement
public String getCost(){
return cost;
}
}
package com.mytest.jxb;
导入java.util.ArrayList;
导入java.util.List;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.xmlementref;
导入javax.xml.bind.annotation.XmlElementWrapper;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.xmlsee;
@XmlRootElement(name=“客户”)
@XMLSEEALLO({Hobby.class})
公共类客户{
字符串名;
智力年龄;
int-id;
@xmlementref
私有列表hobbyList=newarraylist();
公众客户(){
//hobbyList=新的ArrayList();
}
//@XmlElement
公共字符串getName(){
返回名称;
}
@XmlElement
公共void集合名(字符串名){
this.name=名称;
}
//@XmlElement
公共整数getAge(){
回归年龄;
}
@XmlElement
公共无效设置(整数){
这个。年龄=年龄;
}
//@XmlAttribute
公共int getId(){
返回id;
}
@XmlAttribute
公共无效集合id(内部id){
this.id=id;
}
公众爱好(爱好h){
添加(h);
}
@XmlElementWrapper(name=“嗜好”)
@xmlement(name=“hobby”)
公众兴趣爱好列表(){
回归霍布斯主义;
}
}
包com.mytest.jxb;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
课堂爱好{
私有字符串名称;
私有字符串成本;
公共爱好{
}
公共爱好(字符串名称、字符串成本){
this.name=名称;
成本=成本;
}
//@XmlElement
public void setName(){
this.name=名称;
}
@XmlElement
公共字符串getName(){
返回名称;
}
//@XmlElement
公共成本{
成本=成本;
}
@XmlElement
公共字符串getCost(){
退货成本;
}
}
拥有addHobby(Hobby h)
对于JAXB来说是不够的。您的类应该是真正的POJO,并且应该具有void sethabiods(List hobbyList){this.hobbyList=hobbyList;}
。另外,我认为您可以安全地从hobbyList
字段中删除@xmlementref
编辑:我已经检查了您的类并创建了正常工作的版本:
package test;
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.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@XmlRootElement(name = "customer")
public class Customer {
String name;
int age;
int id;
private List<Hobby> hobbyList = new ArrayList<Hobby>();
public Customer() {
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void addHobby(Hobby h) {
hobbyList.add(h);
}
@XmlElementWrapper(name = "hobbies")
@XmlElement(name = "hobby")
public List<Hobby> getHobbies() {
return hobbyList;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
package test;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@XmlRootElement
class Hobby {
private String name;
private String cost;
public Hobby() {
}
public Hobby(String name, String cost) {
this.name = name;
this.cost = cost;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getName() {
return name;
}
public void setCost(String cost) {
this.cost = cost;
}
@XmlElement
public String getCost() {
return cost;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
封装测试;
导入java.util.ArrayList;
导入java.util.List;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.xmlementref;
导入javax.xml.bind.annotation.XmlElementWrapper;
导入javax.xml.bind.annotation.XmlRootElement;
导入org.apache.commons.lang.builder.ToStringBuilder;
导入org.apache.commons.lang.builder.toString样式;
@XmlRootElement(name=“客户”)
公共类客户{
字符串名;
智力年龄;
int-id;
私有列表hobbyList=newarraylist();
公众客户(){
}
@XmlElement
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@XmlElement
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
@XmlAttribute
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公众爱好(爱好h){
添加(h);
}
@XmlElementWrapper(name=“嗜好”)
@xmlement(name=“hobby”)
公众兴趣爱好列表(){
回归霍布斯主义;
}
@凌驾
公共字符串toString(){
返回ToStringBuilder.reflectionString(这是ToStringStyle.SHORT\u PREFIX\u样式);
}
}
包装试验;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入org.apache.commons.lang.builder.ToStringBuilder;
导入org.apache.commons.lang.builder.toString样式;
@XmlRootElement
课堂爱好{
私有字符串名称;
私有字符串成本;
公共爱好{
}
公共爱好(字符串名称、字符串成本){
this.name=名称;
成本=成本;
}
公共void集合名(字符串名){
this.name=名称;
}
@XmlElement
公共字符串getName(){
返回名称;
}
公共无效设置成本(字符串成本){
成本=成本;
}
@XmlElement
公共字符串getCost(){
退货成本;
}
@凌驾
公共字符串toString(){
返回ToStringBuilder.reflectionString(这是ToStringStyle.SHORT\u PREFIX\u样式);
}
}