2

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;


    }
Arpan Banerjee
  • 826
  • 12
  • 25

2 Answers2

3

The simplest way is to create a CustomerList DTO that holds the list of CustomerDtos.

public class CustomerList {

    @JacksonXmlElementWrapper(localName = "CustomerList")
    @JacksonXmlProperty(localName = "Customer")
    List<CustomerDto> list;
}

More examples can be found here: https://mincong.io/2019/03/19/jackson-xml-mapper/

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
2

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/

Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • I have just noticed one more issue here, if look at the `lastName` and `gender` tags--no values are comming up. I am not able to find out the reason. – Arpan Banerjee Apr 23 '20 at 15:06
  • 1
    did you check logging in CustomerDto data is set properly or not and add customer entity also – Eklavya Apr 23 '20 at 15:11
  • yes, it was my bad, I got it. I was setting it inaccurately in the converter class. – Arpan Banerjee Apr 23 '20 at 15:24
  • can you pls have a look at it, Its from the same project. Here is the link [https://stackoverflow.com/questions/61393839/how-can-i-send-data-in-post-body-without-setting-the-auto-generated-primary-key] – Arpan Banerjee Apr 24 '20 at 07:52