4

I'm using Spring 4.x and I have following RestController method which should return list of all flights

@RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = RequestMethod.GET)
public FlightWrapper returnAllFlights() {
    List<FlightDto> flights = data.findAll();

    return new FlightWrapper(flights);
}

FlightWrapper class looks like this (rootElement = flights element = flight):

@XmlRootElement(name = "flights")
public class FlightWrapper {

    private List<FlightDto> flights;

    public FlightWrapper() {}

    public FlightWrapper(List<FlightDto> flights) {
        this.flights = flights;
    }

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

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

The problem is when I call returnAllFlights() it will return xml in this format:

<FlightWrapper>
    <flights>
        <flights>
            ....
        </flights>
        <flights>
            ....
        </flights>
    </flights>
</FlightWrapper>

I expected that single flight should have tag flight and whole list of flights should be flights however as you can see items in list have the same tag as list itself.

Any idea how to fix it ?

Martin Čuka
  • 16,134
  • 4
  • 23
  • 49
  • Clean and Rebuild your code, it is practically almost the same than a previous project I had. Could you add the OXM or `Marshaller` technology configuration?. Your controller is `@RestController`? – Manuel Jordan Oct 09 '16 at 22:18
  • Yes my controller is @RestController I am using spring boot 1.4.1. For xml support I just added dependency to maven jackson-dataformat-xml – Martin Čuka Oct 09 '16 at 22:39

1 Answers1

8

According with your comments since you are using jackson-dataformat-xml module the JAXB annotations now are ignored. You must update your class to use these annotations.

@JacksonXmlRootElement(localName="flights")
public class FlightWrapper {

    private List<FlightDto> flights;

    public FlightWrapper() {}

    public FlightWrapper(List<FlightDto> flights) {
        this.flights = flights;
    }

    @JacksonXmlElementWrapper(useWrapping=false)
    @JacksonXmlProperty(localName="flight")        
    public List<FlightDto> getFlights() {
        return flights;
    }

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

I had the same problem than you but through Spring Framework, not through Spring Boot. But that behaviour happens when the jackson-dataformat-xml module is added into the classpath. It according with my experience.

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158