Orika data mapping of list of entity to list of DTO within a response (wrapper) object

1.9k views Asked by At

I'll admit right off the bat that I'm new to Orika and I'm still learning how it works and the in/out of it.

The situation I find myself in is a web service called by an EmberJS web client which returns a list of Address (JSON REST Object). The problem I have is that the Response I have to return is a bit of a shell/wrapper object which simply contains a List. I retrieve this list from the DAO and need to map it to the list within the response object.

Mapping the list of entities to the list of dto is easy; that I can do. But I can't quite figure how to map that list of entities to the response object or rather the list of dto within this response object.

I've successfully done this:

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

But how can I specify to the mapperFactory that I want to map a List to List that are to be set into the AddressesResponse.addresses property?

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

In my Java web service, this is a return value of:

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

}
1

There are 1 answers

0
Sidi On

I think there is no what to do it automatically in Orika (because there is no need I think). What you've done so far is fine