MapStruct: Object.class to Custom.class mapping

安稳与你 提交于 2019-12-24 12:50:31

问题


I'm a newbie with MapStruct, and need some help with that.

I have a Source class, with an attribute

Object input;

Which, in runtime, returns a custom object named TicketDetails.

Now, in my target class there is a attribute named,

MyTicket myTicket;

which, I need to map with an attribute of TicketDetails object. For, better understanding, I'm writing the normal java code example below.

SourceClassModel sourceClassModel = new SourceClassModel();
TargetClassModel targetClassModel = new TargetClassModel();

//mapping
TicketDetails ticketDetails = (TicketDetails) sourceClassModel.getInput();
targetClassModel.setMyTicket(ticketDetails.getMyTicket);

Now, my question is, how to achieve this case using MapStruct?


回答1:


Either on a used mapper (see @Mapper#uses()) or in a non-abstract method on the mapper itself - in case it is an abstract class and not an interface - define the mapping from Object to TicketDetails yourself:

TicketDetails asTicketDetails(Object details) {
    return (TicketDetails) details;
}

The generated method for the conversion of SourceClassModel to TargetClassModel will then invoke that manually written method for converting the myTicket property.



来源:https://stackoverflow.com/questions/33994644/mapstruct-object-class-to-custom-class-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!