0

Input

I have these PageAndData object as Targetmapstruct

public class PageAndData {
    Page page;
    List<Data> data;
}
public class Page {
    private int size;
    private int totalPages;
}
public class Data{
    Integer id;
    String name;
    List <Wallet> wallets;
}
public Wallet{
    Integer walletId;
    String walletName;
}

I have these SearchResult as Source

public class SearchResult{
    PageInfo pageInfo;
    List<PageData> pageData;
}
public class PageInfo{
    private int size;
    private int totalPages;
}
public class PageData{
    Integer id;
    String name;
    List<Bag> bags;
}
public Bag{
    Integer bagId;
    String bagName;
}

Question Using Mapstruct to map values from SearchResult -> PageAndData


@Mapper // current impl, partailly ok
public interface searchResultToPageAndData{

    @Mapping(source = "pageInfo", target = "info")
    @Mapping(source = "pageData", target = "data")
    
    PageAndData mymap(SearchResult searchResult);

}

Output expectation

The values from SearchResult should go to PageAndData, expected mapping as follow

//
SearchResult.pageInfo.size -> PageAndData.info.size
SearchResult.pageInfo.totalPages -> PageAndData.info.totalPages
    SearchResult.PageData.id -> PageAndData.data.id
    SearchResult.PageData.name -> PageAndData.data.name
    SearchResult.PageData.bag -> PageAndData.data.id.wallet
        //for every element of array
        SearchResult.PageData.bag.bagId -> PageAndData.data.id.walletId
        SearchResult.PageData.bag.bagName -> PageAndData.data.id.walletName

Example :

i should get data from bag to wallet with change in key name of bag to wallet with change in nested keynames too

SearchResult{
    pageInfo{
        size : 5
        totalPages : 10
    }
    pageData{
        id : 1
        name : "Page101"
        bag:[
            {
            bagId : 1
            bagName : "bagname"
            }
        ]
    }
}
This shud get mapped to
PageAndData{
    info{
        size : 5
        totalPages : 10
    }
    data{
        id : 1
        name : "Page101"
        wallet:[
        {
            walletId : 1
            walletName : "bagname"
        }
        ]
    }
}
3
  • implemeted example if possible Commented May 5 at 22:03
  • Please edit the post and clarify what the actual question is. Commented May 5 at 22:09
  • @Turing85, i tried explaining it again. in simple language i want to map values from one object to another using mapstruct, the catch is - the attribute names are not same (like bagid->walletid) and it is inside an array.. Commented May 6 at 8:23

1 Answer 1

0

You need to tell MapStruct that it should map from PageData to Data and from Bag to Wallet (and how to do the mapping).

The easiest way (IMHO) is to add methods to your mapper:

@Mapper // current impl, partailly ok
public interface searchResultToPageAndData{

    @Mapping(source = "pageInfo", target = "info")
    @Mapping(source = "pageData", target = "data")
    PageAndData mymap(SearchResult searchResult);

    @Mapping(source = "bags", target = "wallets")
    Data map(PageData pageData);

    @Mapping(source = "bagId", target = "walletId")
    @Mapping(source = "bagName", target = "walletName")
    Wallet map(Bag bag);
}
Sign up to request clarification or add additional context in comments.

2 Comments

is it not better to use uses like this @Mapper(uses=SomeClass.class) and this SomeClasss will get next level of Mapping and then again use uses to third and final level of Mapping ?
@sudarshan.spectrum I prefer one mapper for a group of related classes on the reason that a service might need to do mappings between SearchResult and PageAndData as well as doing mappings between PageData and Data. If you have these mappers distributed over several classes you might need to inject all those mappers into the a service where you need to inject only a single mapper if you group them together. IMHO either way is fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.