以下 XML 的 JAXB 注释

发布于 2024-12-09 05:41:20 字数 1332 浏览 1 评论 0原文

我花了一段时间试图让它发挥作用,但运气不佳。

我有以下 XML:

<message>
  <buckets>
    <bucket>
      <channels>
         <channel>Test A</channel>
         <channel>Test B</channel>
      </channels>
      <messageText>This is sample text</messageText>
    </bucket>
    <bucket>
      ....
    </bucket>
  </buckets>
  <userId>10</userId>
 </message>

我试图使用 JAXB 注释将其映射到 POJO,但遇到了麻烦。

我的课程如下:

@XmlRootElement(name="message")
public class MessageRS {

  public static class Bucket {
    private List<String>channels;
    private String text;

    private Bucket() {}

    public List<String> getChannels() { .... }
    public void setChannels(List<String> channels) { .... }
    public String getText() { .... }
    public void setText(String text) { .... }
  }

  private List<Bucket> buckets;

  private Long userId;

  private MessageRS() { }

  public List<Bucket getBuckets() { .... }
  public void setBuckets(List<Bucket> buckets) { .... }
  public long geUserId() { .... }
  public void setUserId(long UserId { .... }
}

不幸的是,这似乎无法正常工作。我将如何注释它以正确地将 XML 映射到该对象(所需的映射应该是显而易见的)?

这里没有什么是一成不变的——此时我可以随意更改 XML 和 Java 类结构。

I've spent a while trying to get this to work however I've had little luck.

I have the following XML:

<message>
  <buckets>
    <bucket>
      <channels>
         <channel>Test A</channel>
         <channel>Test B</channel>
      </channels>
      <messageText>This is sample text</messageText>
    </bucket>
    <bucket>
      ....
    </bucket>
  </buckets>
  <userId>10</userId>
 </message>

I'm trying to get this mapped to a POJO using JAXB annotations but am having trouble.

My class is as follows:

@XmlRootElement(name="message")
public class MessageRS {

  public static class Bucket {
    private List<String>channels;
    private String text;

    private Bucket() {}

    public List<String> getChannels() { .... }
    public void setChannels(List<String> channels) { .... }
    public String getText() { .... }
    public void setText(String text) { .... }
  }

  private List<Bucket> buckets;

  private Long userId;

  private MessageRS() { }

  public List<Bucket getBuckets() { .... }
  public void setBuckets(List<Bucket> buckets) { .... }
  public long geUserId() { .... }
  public void setUserId(long UserId { .... }
}

Unfortunately, this doesn't seem to be working correctly. How would I annotate this to correctly map the XML to this object (the desired mapping should be obvious)?

Nothing is set in stone here - I'm free to change both the XML and Java class structures at this point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-12-16 05:41:20

试试这个:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

public class JaxbStuff {
    public static void main(String[] args) throws Exception {
        MessageRS message = new MessageRS();
        message.setUserId(10);
        MessageRS.Bucket bucket1 = new MessageRS.Bucket();
        bucket1.setText("This is sample text");
        bucket1.setChannels(Arrays.asList("Test A", "Test B"));
        MessageRS.Bucket bucket2 = new MessageRS.Bucket();
        bucket2.setText("Some more text");
        bucket2.setChannels(Arrays.asList("1", "2"));
        message.setBuckets(Arrays.asList(bucket1, bucket2));
        StringWriter writer = new StringWriter();
        JAXBContext.newInstance(MessageRS.class).createMarshaller().marshal(message, writer);
        System.out.println(writer);
    }

    @XmlRootElement(name = "message")
    static class MessageRS {

        public static class Bucket {
            private List<String> channels;
            private String text;

            private Bucket() {}

            @XmlElementWrapper(name = "channels")
            @XmlElement(name = "channel")
            public List<String> getChannels() {
                return channels;
            }

            public void setChannels(List<String> channels) {
                this.channels = channels;
            }

            public String getText() {
                return text;
            }

            public void setText(String text) {
                this.text = text;
            }
        }

        private List<Bucket> buckets;

        private Long userId;

        private MessageRS() { }

        public List<Bucket> getBuckets() {
            return buckets;
        }

        public void setBuckets(List<Bucket> buckets) {
            this.buckets = buckets;
        }

        public long getUserId() {
            return userId;
        }

        public void setUserId(long UserId) {
            this.userId = UserId;
        }
    }

}

您在 getUserId() 中也有一个拼写错误,这会导致该属性无法正常工作。它是 geUserId()。

而且,像这样混合 Long 和 long 是很危险的。如果您的 userId 为 null 并且您调用 getUserId(),它将抛出 NullPointerException。

Try this:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

public class JaxbStuff {
    public static void main(String[] args) throws Exception {
        MessageRS message = new MessageRS();
        message.setUserId(10);
        MessageRS.Bucket bucket1 = new MessageRS.Bucket();
        bucket1.setText("This is sample text");
        bucket1.setChannels(Arrays.asList("Test A", "Test B"));
        MessageRS.Bucket bucket2 = new MessageRS.Bucket();
        bucket2.setText("Some more text");
        bucket2.setChannels(Arrays.asList("1", "2"));
        message.setBuckets(Arrays.asList(bucket1, bucket2));
        StringWriter writer = new StringWriter();
        JAXBContext.newInstance(MessageRS.class).createMarshaller().marshal(message, writer);
        System.out.println(writer);
    }

    @XmlRootElement(name = "message")
    static class MessageRS {

        public static class Bucket {
            private List<String> channels;
            private String text;

            private Bucket() {}

            @XmlElementWrapper(name = "channels")
            @XmlElement(name = "channel")
            public List<String> getChannels() {
                return channels;
            }

            public void setChannels(List<String> channels) {
                this.channels = channels;
            }

            public String getText() {
                return text;
            }

            public void setText(String text) {
                this.text = text;
            }
        }

        private List<Bucket> buckets;

        private Long userId;

        private MessageRS() { }

        public List<Bucket> getBuckets() {
            return buckets;
        }

        public void setBuckets(List<Bucket> buckets) {
            this.buckets = buckets;
        }

        public long getUserId() {
            return userId;
        }

        public void setUserId(long UserId) {
            this.userId = UserId;
        }
    }

}

You also had a typo in getUserId(), which would make that property not work correctly. It was geUserId().

Also, it's dangerous to mix Long and long like that. If your userId is null and you call getUserId(), it'll throw a NullPointerException.

肩上的翅膀 2024-12-16 05:41:20

通常,jaxb 在列表周围使用包装对象。因此,您将拥有一个 Buckets 类,其中包含 List; getBuckets(),并且您将拥有一个带有 List的 Channels 类。 getChannels()。我相信您可以使用额外的注释来避免额外的包装类,但它们并不经常使用(至少不被常见的自动生成工具使用)。

就我个人而言,我发现编写 xml 模式并使用 xjc 生成 java 类要简单得多。

typically, jaxb uses wrapper objects around lists. so, you would have a Buckets class which has List<Bucket> getBuckets(), and you would have a Channels class with List<Channel> getChannels(). i believe there are extra annotations you can use to avoid the extra wrapper classes, but they are not frequently used (at least not by the common auto-generation tools).

personally, i find it much simpler to write the xml schema and use xjc to generate the java classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文