You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by ravi sankar <sa...@yahoo.co.in.INVALID> on 2016/05/02 14:31:22 UTC

JAXB - JaxRs parsing issue

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

Re: JAXB - JaxRs parsing issue

Posted by Romain Manni-Bucau <rm...@gmail.com>.
maybe try returning People instead of a list of person ;)


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2016-05-02 14:31 GMT+02:00 ravi sankar <sa...@yahoo.co.in.invalid>:

> 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
>