gpt4 book ai didi

java - Orika 响应(包装)对象中实体列表到 DTO 列表的数据映射

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:19 29 4
gpt4 key购买 nike

我马上承认我对 Orika 还很陌生,而且我仍在学习它的工作原理以及它的内部/外部。

我发现自己所处的情况是由 EmberJS Web 客户端调用的 Web 服务,该客户端返回地址列表(JSON REST 对象)。我遇到的问题是,我必须返回的响应是一个外壳/包装对象,它只包含一个列表。我从 DAO 检索此列表,并需要将其映射到响应对象内的列表。

将实体列表映射到 dto 列表很容易;我能做的。但我不太清楚如何将该实体列表映射到响应对象,或者更确切地说,映射到该响应对象中的 dto 列表。

我已经成功地做到了这一点:

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(AddressVO.class, Address.class).byDefault().register();

MapperFacade mapper = mapperFactory.getMapperFacade();
List<Address> addressesRestDto = mapper.mapAsList(addressesVO, Address.class);

response = new AddressesResponse();
response.setAddresses(addressesRestDto);

return new ResponseEntity<AddressesResponse>(response, HttpStatus.OK);

但是我如何向mapperFactory指定我想要将列表映射到要设置到AddressesResponse.addresses属性中的列表?

{
"addresses" : [{
"id" : 1,
"type" : "WORK",
"street" : "6262 Sunset Drive",
"city" : "Miami",
"state" : "FL",
"zip" : "33143"
}, {
"id" : 2,
"type" : "HOME",
"street" : "101 Main Street",
"city" : "New York",
"state" : "NY",
"zip" : "10022"
}
]
}

在我的 Java Web 服务中,返回值为:

@RequestMapping(value = "/{id}/addresses", method = RequestMethod.GET)
public ResponseEntity<AddressesResponse> retrieveContactAddresses(@PathVariable int id)
{
....
List<AddressVO> addressesVO = contactDAO.retrieveAddresses(id);
....
}
public class AddressesResponse 
implements Serializable
{

private static final long serialVersionUID = -4734662942482137495L;

protected List<Address> addresses;


public List<Address> getAddresses() {
return addresses;
}

public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}


@JsonInclude(Include.NON_NULL) // Ensure that only not null values gets returned in the JSON document
public class Address
extends UpdateableRestDto
{

private static final long serialVersionUID = 4332153509500388664L;

protected String type;

protected String street;

protected String city;

protected String state;

protected String zip;


public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

}


public class AddressVO
extends UpdateableVO
{

private static final long serialVersionUID = -2536056035043485871L;

protected AddressTypeEnum type;

protected String street;

protected String city;

protected String state;

protected String zip;


public AddressTypeEnum getType() {
return type;
}

public void setType(AddressTypeEnum type) {
this.type = type;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

}

最佳答案

我认为 Orika 中没有自动执行的操作(因为我认为没有必要)。到目前为止你所做的一切都很好

关于java - Orika 响应(包装)对象中实体列表到 DTO 列表的数据映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159823/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com