javaspringspring-bootspring-restxml-binding

How to map REST output to a Dto in Spring Boot using @XmlElement annotation , so that i can get the xml output in desired format?


I have two classes/tables--- Customer and Address having a bi-directional one-to-one relationship.

I getting the details from these two tables and exposing them using a rest controller and i am getting the following output.

enter image description here

But instead of <List> and <item> tags i want <CustomerList> and <Customer> respectively.Like this--

<CustomerList>
   <Customer>
      <id>1</id>
      <firstName>Banerjee</firstName>
      <lastName/>
      <gender/>
      <date>2012-01-26T09:00:00.000+0000</date>
      <addressdto>
          <id>1</id>
          <city>Purulia</city>
          <country>Indiia</country>
      </addressdto>
   </Customer>
  ...........

controller class

@RestController
public class HomeController {

    @Autowired
    private CustomerService customerService;

    @GetMapping(path="/customers",produces= {"application/xml"})
    public List<CustomerDto> getCustomers(){
        List<CustomerDto> cusDtoList=new ArrayList<>();
    cusDtoList=customerService.getCustomers();
        return cusDtoList;
    }

Service Class

@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private EntityToDtoMapper entityToDto;

    public List<CustomerDto> getCustomers(){
        List<Customer>customerList=customerRepository.findAll();
        //CustomerDtoList customerDtoList=new CustomerDtoList();
        List<CustomerDto> cusDtoList=new ArrayList<>();
        for (Customer customer : customerList) {
            CustomerDto customerDto=entityToDto.mapToDto(customer);
            //customerDtoList.addCustomerDto(customerDto);
            cusDtoList.add(customerDto);
        }
        return cusDtoList;
    }

AddressDto


@JsonIgnoreProperties(ignoreUnknown=true)
public class AddressDto {

    private int id;
    private String city;
    private String country;

...getter/settters and no arg cons/ no annotations
}

CustomerDto

@XmlRootElement
@JsonIgnoreProperties(ignoreUnknown=true)
public class CustomerDto {

    private int id;
    private String firstName;
    private String lastName;
    private String gender;
    private Date date;
    private AddressDto addressdto;

    public CustomerDto() {
        super();
    }

    @XmlElement
    public AddressDto getAddressdto() {
        return addressdto;
    }
...other getter/setters..no annotations

MaptoDto class

@Component
public class EntityToDtoMapper {

    public CustomerDto mapToDto(Customer customer) {
   **getting frm customer and setting it to dto**
        return customerDto;


    }

Solution

  • Use @JacksonXmlRootElementannotation to set the name for the XML output.

    @JacksonXmlRootElement(localName = "CustomerList")
    public class CustomerDTOList {
    
        @JacksonXmlProperty(localName = "Customer")
        @JacksonXmlElementWrapper(useWrapping = false)
        List<CustomerDto> list;
    }
    

    With @JacksonXmlProperty and @JacksonXmlElementWrapper annotations we ensure that we have Customer elements nested in the CustomerList element for an ArrayList of Customer objects. The CustomerDTOList bean is a helper bean which is used to get nicer XML output.

    @JacksonXmlRootElement(localName = "Customer")
    public class CustomerDto {
    

    For more details http://zetcode.com/springboot/restxml/