关于REST响应和XMLElement [英] Regarding REST response and XMLElement

查看:87
本文介绍了关于REST响应和XMLElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在代码中创建的REST响应:

I have below a REST response which needs to be created in the code:

<sample>
   <tags> 
       <tag>
           <name>ABC</name>
           <Date>2014-10-14T12:30:05Z</ingress>
       </tag>
       <tag>
           <name>DEF</name>
           <Date>2014-10-14T12:30:05Z</ingress>
       </tag>
   </tags>
</sample>

但是,我得到了

<sample>
    <tags>           
        <name>ABC</name>
        <Date>2014-10-14T12:30:05Z</ingress>
    </tags>
    <tags>
        <name>DEF</name>
        <Date>2014-10-14T12:30:05Z</ingress>        
    </tags>
</sample>

有人请帮我告诉Java类如何获得所需的REST响应?

in the response.Can someone please help me how the declaration of Java class to get the desired REST response ?

这是java代码:

@XmlRootElement(name = "sample")
public class Sample {
    private List<Tag> tags;

    @XmlElement(name = "tags")
    public List<Tag> getTags() {
        return tags;
    }

    /**
     * @param tags
     *            the tags to set
     */
    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }

}

@XmlRootElement(name = "tag")
public class Tag {
    private String name;
    private Date date;

    /**
     * @return the name
     */
    @XmlElement(name = "name")
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the date
     */
    @XmlElement(name = "date")
    public Date getDate() {
        return date;
    }

    /**
     * @param date
     *            the date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }
}

谢谢

推荐答案

@XmlElement(name = "tags")
List<Tag> tags;

它基本上读取列表中的每个项目,创建一个名为的元素<标记> 。所以从本质上讲,你所拥有的只是< subject> 元素包装多个< tags>

It basically reads, for each item in the list, create a element named <tags>. So in essence, all you have is a <subject> element wrapping multiple <tags>.

获得另一个上层元素的几个选项

A couple options to get another "upper-level" element

你可以创建一个上层元素代表那个的类,比如标签

You can create a "upper-level" class to represent that, say Tags

public class Tags {
    protected List<Tag> tags;

    @XmlElement(name = "tag")
    public List<Tag> getTags() {
        if (tags == null) {
            tags = new ArrayList<>();
        }
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }
}

然后有一个的实例标签作为的属性<示例

@XmlRootElement(name = "sample")
public class Sample {
    private Tags tags;

    @XmlElement(name = "tags")
    public void setTags(Tags tags) {
        this.tags = tags;
    }

    public Tags getTags() {
        return tags;
    }
}

OR

更简单的解决方案就是使用 @XmlElementWrapper

An even simpler solution is just to use @XmlElementWrapper


围绕XML表示生成包装元素。这主要用于生成围绕集合的包装器XML元素

Generates a wrapper element around XML representation. This is primarily intended to be used to produce a wrapper XML element around collections

使用原始代码,您可以简单地将注释添加到列表

Using your original code, you can simple add the annotation to the list

@XmlRootElement(name = "sample")
public class Sample {
   private List<Tag> tags;

    @XmlElementWrapper(name = "tags")
    @XmlElement(name = "tag")
    public List<Tag> getTags() {
        if (tags == null) {
            tags = new ArrayList<>();
        }
        return tags;
    }

    public void setTags(List<Tag> tags) {
        this.tags = tags;
    }
}

这篇关于关于REST响应和XMLElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆