users@jersey.java.net

[Jersey] Re: Problem with getting data from a list with Jersey

From: Arul Dhesiaseelan <aruld_at_acm.org>
Date: Sun, 12 Jun 2011 21:26:25 -0600

I believe you just need to add @XmlRootElement to your Airport class to fix
the JAXB exception. This should work:

@XmlRootElement
public class Airport {

  private List<Flight> flights;

  @XmlElementWrapper(name = "flightList")
  @XmlElement(name = "flight")
  public List<Flight> getFlights() {
    return flights;
  }

  public void setFlights(List<Flight> flights) {
    this.flights = flights;
  }

  public static class Flight {
    private String name;

    public String getName() {
      return name;
    }

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

}

-Arul

On Fri, Jun 10, 2011 at 11:45 AM, Eric Reagan <reaganej_at_gmail.com> wrote:

> Hello,
> I have a class which is like
> @XmlAccessorType( XmlAccessType.PUBLIC_MEMBER )
> public class Airport
> {
> ...
>
> private List<Flight> flights;
>
> ...
> @XmlElementWrapper(name="flightList")
> @XmlElement(name="flight})
> private List<Flight> flights;
>
> ...
>
> public List<Flight> getFlights()
> {
> return flights;
> }
>
> public void setFlights(List<Flight> flights)
> {
> this.flights = flights;
> }
>
>
> }
>
> and I am getting a class has two properties of the same name about the
> getFlights() method and the flights variable. The interesting problem I am
> having is in my GUI code when I call getFlights() method I have the class
> has two properties of the same name error. If I change my code to look like
>
> public class Airport
> {
> ...
>
> private List<Flight> flights;
>
> ...
> @XmlElementWrapper(name="flightList")
> @XmlElement(name="flight})
> private List<Flight> flights;
>
> ...
>
> public List<Flight> getFlights()
> {
> return flights;
> }
>
> public void setFlights(List<Flight> flights)
> {
> this.flights = flights;
> }
>
>
> }
>
>
> Everything seams to work fine but the getFlights() will always return
> an empty list. My flight class looks like
> @XmlRootElement
> public class Flight
> {
> ...
> }
>
> I don't understand what I am doing wrong when trying to my Airport
> object via a Jersey GET request and either I don't get a list of flights or
> I get a class has two properties of the same name exception.
>
> Thank you,
>
> --
> Eric Reagan
>