JAXB不会对我的列表进行编组

时间:2012-11-12 19:24:55

标签: java java-ee jaxb

我有以下两个课程。有什么理由说JAXB没有将答案列表编组到XML中吗?我遗漏了正确编组的其他属性。是的,填充了答案列表,但未显示答案元素。

public class Question {

    private List<Answer> answers;

    public List<Answer> getAnswers() {
        return answers;
    }

    public void setAnswers(final List<Answer> answers) {
        this.answers = answers;
    }
}

和测验对象

@XmlRootElement
public class Quiz {

    private List<Question> questions;

    public Quiz() {
        questions = new ArrayList<Question>();
    }

    public List<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }
}

1 个答案:

答案 0 :(得分:1)

你为我模拟的作品。见下面的例子:

<强>演示

package forum13350129;

import java.util.*;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Quiz.class);

        Question question1 = new Question();
        List<Answer> answers1 = new ArrayList();
        answers1.add(new Answer());
        answers1.add(new Answer());
        question1.setAnswers(answers1);

        Question question2 = new Question();
        List<Answer> answers2 = new ArrayList();
        answers2.add(new Answer());
        answers2.add(new Answer());
        question2.setAnswers(answers2);

        Quiz quiz = new Quiz();
        quiz.getQuestions().add(question1);
        quiz.getQuestions().add(question2);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(quiz, System.out);
    }

}

<强>输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<quiz>
    <questions>
        <answers/>
        <answers/>
    </questions>
    <questions>
        <answers/>
        <answers/>
    </questions>
</quiz>

更新#1

  

如果你试图像我一样把它作为JSON,该怎么办?

这取决于。 JSON绑定不是JAXB (JSR-222)规范的一部分。这意味着您的环境正在执行以下操作之一:

  1. 将JAXB impl与Jettison等库一起使用以生成JSON(请参阅:http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html
  2. 使用JSON绑定库(例如具有一些JAXB注释支持的Jackson)
  3. 使用JAXB impl,例如提供JSON绑定的EclipseLink MOXy(参见:http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html)。
  4. 使用EclipseLink JAXB (MOXy)如下:

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.marshal(quiz, System.out);
    

    会生成以下JSON:

    {
       "quiz" : {
          "questions" : [ {
             "answers" : [ {
             }, {
             } ]
          }, {
             "answers" : [ {
             }, {
             } ]
          } ]
       }
    }
    

    更新#2

      

    我已经解决了延迟加载关系的问题。我在用   存储库中的Hibernate.initialize。我想恢复这个   来自我所拥有的资源的测验对象。我正在使用JAX-RS。

    我看到人们在将Hibernate模型编组为XML时遇到了问题。我认为问题是由于Hibernate使用的代理对象。在下面的链接中,我建议使用XmlAdapter在代理对象和真实对象之间进行转换,这很有帮助。

相关问题