如何使用@XmlElement 注释将 REST 输出映射到 Spring Boot 中的 Dto,以便我可以获得所需格式的 xml 输出?

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?

我有两个classes/tables--- CustomerAddress 具有双向一对一关系。

我从这两个表中获取详细信息并使用休息控制器公开它们,我得到以下输出。

但是我想要 <CustomerList><Customer> respectively.Like 这个--

而不是 <List><item> 标签
<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>
  ...........

控制器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;
    }

服务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;


    }

最简单的方法是创建一个包含 CustomerDto 列表的 CustomerList DTO。

public class CustomerList {

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

可以在此处找到更多示例:https://mincong.io/2019/03/19/jackson-xml-mapper/

使用 @JacksonXmlRootElement 注释设置 XML 输出的名称。

@JacksonXmlRootElement(localName = "CustomerList")
public class CustomerDTOList {

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

使用@JacksonXmlProperty 和@JacksonXmlElementWrapper 注释,我们确保我们将Customer 元素嵌套在Customer 对象的ArrayList 的CustomerList 元素中。 CustomerDTOList bean 是一个辅助 bean,用于获得更好的 XML 输出。

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

更多详情http://zetcode.com/springboot/restxml/