2

我有两个类/表—— 客户地址具有双向的一对一关系。

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

在此处输入图像描述

但不是我想要的<List><item>标签<CustomerList>,而是<Customer>分别。像这样--

<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>
  ...........

控制器类

@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
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;
    }

地址Dto


@JsonIgnoreProperties(ignoreUnknown=true)
public class AddressDto {

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

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

客户Dto

@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 类

@Component
public class EntityToDtoMapper {

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


    }
4

2 回答 2

3

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

public class CustomerList {

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

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

于 2020-04-18T09:04:38.393 回答
2

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

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

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

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

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

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

于 2020-04-18T09:11:51.577 回答