spring-boot - MapStruct:如何使用 mapstruct 将 String 转换为 byte[]
问题描述
在我的 dto 课程中:
private String password;
在我的模型课上:
private byte[] password;
我想使用 mapStruct 将 String 转换为 byte[]。有人可以帮忙吗
提前致谢。
解决方案
最好的方法是为String
和之间的映射提供默认方法byte[]
。
例如:
@Mapper
public MyMapper {
Model fromDto(Dto dto);
default byte[] toBytes(String string) {
return string != null ? string.getBytes() : null;
}
}
有了这个,您将让 MapStruct 为您之间的所有其他字段自动执行Dto
和并将Model
映射保留在方法之间。String
byte[]
toBytes
推荐阅读
- javascript - 如何正确更新 redux 商店中的道具?
- r - 基于 ID 列向数据框添加值列的更简单的 R 代码
- html - Bootstrap 4 响应式图像(imag-fluid)的问题
- javascript - 密码中未包含所有用户输入条件的问题
- ios - 在 SwiftUI 中创建底部主视图栏
- c# - 为什么当我在 Revit 上关闭 Windows 窗体插件中的子表单时,它会关闭整个插件?
- arrays - 如何创建指向多维数组的指针?
- java - 如何以编程方式将边距中的 px 转换为 dp?
- java - 使用 java bufferedReader 和 Scanner 解析文本文件
- linux - Ansible:检查文件文本,如果没有,添加它