简体   繁体   English

MapStruct DTO 属性

[英]MapStruct DTO property

I need to have a property on my DTO like idEncrypted because they can pass me only encrypted, however, I need to map the decrypt id as I find on DB.我需要在我的 DTO 上有一个属性,比如 idEncrypted,因为它们只能通过加密传递给我,但是,我需要 map 解密 ID,就像我在 DB 上找到的那样。 I already have a decrypt method, but I don't know how to map it and ignore the idEncrypted.我已经有一个解密方法,但我不知道如何 map 它并忽略 idEncrypted。

@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
public class MyDTO {  

    private String idEncrypted;

    ...
    ...

}

I don't know where to do the conversion我不知道在哪里进行转换

private Long idDecrypted = Long.parseLong(MyUtils.decrypt(idEncrypted));

You can write your own custom qualified method for doing the decryption.您可以编写自己的自定义限定方法来进行解密。

eg例如

@Mapper
public MyMapper {

    @Mapping(target = "id", source = "idEncrypted", qualifiedByName = "decryptId")
    MyEntity map(MyDTO dto);

    @Named("decryptId")
    default Long decryptId(String id) {
        return id != null ? Long.parseLong(MyUtils.decrypt(id)) : null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM