tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Manni-Bucau <rmannibu...@gmail.com>
Subject Re: JAXB - JaxRs parsing issue
Date Mon, 02 May 2016 12:39:46 GMT
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 <sankar_ravi_c@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
>

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