tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ravi sankar <sankar_rav...@yahoo.co.in.INVALID>
Subject JAXB - JaxRs parsing issue
Date Mon, 02 May 2016 12:31:22 GMT
Issue is exists in tomee 1.7.4 & tomee 7.0.0-M3

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class People extends ArrayList<Person> {

    private static final long serialVersionUID = 1L;
    
    @XmlElement(name = "person")
    public List<Person> getPeople() {
        return this;
    }
}

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name + "(" + age + ")";
    }
}
Jaxb Standalone example
public static void main(String[] args) throws JAXBException {
        Marshaller jaxbMarshaller = JAXBContext.newInstance(People.class).createMarshaller();;
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
         People list = new People();
            list.add(new Person("Penny", 1));
        jaxbMarshaller.marshal(list, System.out);
}

Gives
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<people>
    <person>
        <age>1</age>
        <name>Penny</name>
    </person>
</people>

Here root element is people
Jaxrs Service
@Path("persons")
public class MyResource {
    @GET
    @Produces({ "application/xml", "application/json" })
        public
        List<Person> getList() {
        People list = new People();
        list.add(new Person("Penny", 1));
        return list;
    }
}

Gives
<persons>
    <person>
        <age>1</age>
        <name>Penny</name>
    </person>
</persons>

Root element is persons

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message