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

标签 java spring spring-boot spring-rest xml-binding

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

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

enter image description here

但不是 <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>
  ...........

Controller 类

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

客户D至

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


    }

最佳答案

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

public class CustomerList {

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

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

关于java - 如何使用@XmlElement注释将REST输出映射到Spring Boot中的Dto,以便我可以获得所需格式的xml输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61286339/

相关文章:

java - 选择哪个选项来设计此 API 端点

java - VSCode Java 调试器抛出错误 'org.eclipse.jdi.internal.connect.ConnectImpl$StringArgumentImpl.<init>'

java - 如何在各个eclipse插件之间共享依赖JAR?

java - Spring Openid、OpenIDAuthenticationToken

java - OpenNLP 解析器训练

Spring Boot @Formula 在每个结果上显示相同的 likeCount

java - Spring可以自动处理验证错误吗?

java - 在同一 session 中更改实体管理器数据源

Java:如何将RGB颜色转换为CIE Lab

java - 如何结合.java和html?