1

我马上承认我是 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 指定我想要将 List 映射到要设置到 AddressesResponse.addresses 属性中的 List 呢?

{
    "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;
    }

}
4

1 回答 1

0

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

于 2014-11-28T15:25:01.750 回答