Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MapStruct : How to convert String to byte[] using mapstruct

In My dto class :

private String password;

In my model class:

private byte[] password;

I want to convert String to byte[] using mapStruct. Can someone help

Thanks in advance.

like image 266
Pratik Jaiswal Avatar asked Feb 26 '20 18:02

Pratik Jaiswal


1 Answers

Best would be to provide a default method for mapping between String and byte[].

For example:

@Mapper
public MyMapper {

    Model fromDto(Dto dto);

    default byte[] toBytes(String string) {
        return string != null ? string.getBytes() : null;
    }

}

With this you will have MapStruct do automatic for all of your other fields between Dto and Model and leave the mapping between String and byte[] to the toBytes method.

like image 141
Filip Avatar answered Oct 23 '22 10:10

Filip