如何删除GET请求中的一些XML标记

时间:2015-11-13 14:51:03

标签: java xml rest jpa jaxb


我想在浏览器上打印这样的内容:

<measureTypes>
    <measureType>weight</measureType>
    <measureType>height</measureType>
    <measureType>steps</measureType>
    <measureType>bloodpressure</measureType>
</measureTypes>

但是我得到了这个输出:

<measureDefinitions>
    <measureDefinition>
        <measureName>weight</measureName>
    </measureDefinition>
    <measureDefinition>
        <measureName>height</measureName>
    </measureDefinition>
    <measureDefinition>
        <measureName>steps</measureName>
    </measureDefinition>
    <measureDefinition>
        <measureName>blood pressure</measureName>
    </measureDefinition>
    <measureDefinition>
        <measureName>heart rate</measureName>
    </measureDefinition>
    <measureDefinition>
        <measureName>bmi</measureName>
    </measureDefinition>
</measureDefinitions>

以下是我的Model类代码:

@NamedQuery(name="MeasureDefinition.findAll", query="SELECT m FROM MeasureDefinition m")
public static List<MeasureDefinition> getAll() {
    EntityManager em = LifeCoachDao.instance.createEntityManager();

    List<MeasureDefinition> list = em.createNamedQuery("MeasureDefinition.findAll",                    MeasureDefinition.class).getResultList();
    LifeCoachDao.instance.closeConnections(em);
    return list;
}

以下是Resource类中的代码(这是我打印输出的地方):

@GET
@Produces({MediaType.TEXT_XML,  MediaType.APPLICATION_JSON ,  MediaType.APPLICATION_XML })
public List<MeasureDefinition> getMeasuresTypes() {
    EntityManager em = LifeCoachDao.instance.createEntityManager();
    System.out.println("Getting list of measures...");
    List<MeasureDefinition> measures = MeasureDefinition.getAll();
    System.out.println("Measures " + measures);
    System.out.println("Size of the list " + measures.size());
    return measures;
}

因此我的问题是:如何从输出中删除<measureDefinition>?难道我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可能需要围绕该列表创建客户包装器,然后执行以下操作:

@XmlRootElement(name = "measureTypes")
public class MessageDefinitionResult {

    @XmlElement(name = "measureType")
    @XmlJavaTypeAdapter(MeasureDefinitionXmlAdapter.class)
    private List<MeasureDefinition> measureDefinitions;

    public MessageDefinitionResult(final List<MeasureDefinition> measureDefinitions) {
        this.measureDefinitions = measureDefinitions;
    }

    public MessageDefinitionResult() { }

    public List<MeasureDefinition> getMeasureDefinitions() {
        return measureDefinitions;
    }

    public static final class MeasureDefinitionXmlAdapter extends XmlAdapter<String, MeasureDefinition> {

        @Override
        public MeasureDefinition unmarshal(final String v) throws Exception {
            return null; // or whatever
        }

        @Override
        public String marshal(final MeasureDefinition v) throws Exception {
            return v.getName();
        }
    }

}

...

package de.zalando.shop.frontend.springmvc.controller.user.account;

/**
 * @author  mochieng (marembo.isaiah.ochieng@zalando.de)
 * @since   Nov 13, 2015
 */
public class MeasureDefinition {

    private String name;

    private String value;

    private String moreValuesAndProperties;

    public MeasureDefinition(final String name) {
        this.name = name;
    }

    public MeasureDefinition(final String name, final String value, final String moreValuesAndProperties) {
        this.name = name;
        this.value = value;
        this.moreValuesAndProperties = moreValuesAndProperties;
    }

    public MeasureDefinition() { }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public String getMoreValuesAndProperties() {
        return moreValuesAndProperties;
    }

}