问题
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