[英]MapStruct: How to map property from "java.lang.Object to "java.lang.String"
New to MapStrut; MapStrut 的新功能; Object to String Error:
对象到字符串错误:
[ERROR] /util/LicenseMapper.java:[11,23] Can't map property "java.lang.Object license.customFields[].value" to "java.lang.String license.customFields[].value".
[错误] /util/LicenseMapper.java:[11,23] 无法将属性“java.lang.Object license.customFields[].value”映射到“java.lang.String license.customFields[].value”。 Consider to declare/implement a mapping method: "java.lang.String map(java.lang.Object value)".
考虑声明/实现一个映射方法:“java.lang.String map(java.lang.Object value)”。
Code:代码:
@Mapper
public interface LicenseMapper {
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
}
The vo.license contains List of CustomFields having property as vo.license 包含具有属性的 CustomFields 列表
@SerializedName("Value")
@Expose
private Object value;
The Json has input for one field as object as it might come boolean or string or anything so i have mapped it into object. Json 将一个字段的输入作为对象,因为它可能是布尔值或字符串或任何内容,因此我已将其映射到对象中。 Whereas in dao layer has same field in String.
而在 dao 层在 String 中具有相同的字段。 (In custom mapper i just String.valueof but not sure how to achieve it using Mapstrut)
(在自定义映射器中,我只是 String.valueof 但不确定如何使用 Mapstrut 实现它)
Can anyone tell me what settings are required in LicenseMapper to convert Object to String?谁能告诉我 LicenseMapper 需要哪些设置才能将对象转换为字符串?
License Structure - Source and destination:许可证结构 - 来源和目的地:
.
.
private String notes;
private Boolean isIncomplete;
private List<CustomField> customFields = null;
private List<Allocation> allocations = null;
Custom Field Structure in Source (removed gson annotations):源中的自定义字段结构(删除了 gson 注释):
.
.
private String name;
private Object dataType;
private Object value;
Custom FIeld Structure in Destination目标中的自定义字段结构
private String name;
private String datatype;
private String value;
You can try to use annotation @Mapping with expression您可以尝试将注释@Mapping 与表达式一起使用
@Mapping(expression = "java( String.valueOf(source.getValue()) )", target = "value")
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
UPDATE更新
@Mapper
public interface LicenseMapper {
LicenseMapper MAPPING = Mappers.getMapper(LicenseMapper.class);
List<License> entityListToDaoList(List<com.integrator.vo.license.License> source);
License entityToDao(com.integrator.vo.license.License source);
List<CustomField> customFieldListToCustomFieldList(List<*your custom field path*CustomField> source);
@Mapping(expression = "java( String.valueOf(source.getValue()) )", target = "value")
CustomField customFieldToCustomField(*your custom field path*CustomField source);
}
IN YOUR CODE在您的代码中
import static ***.LicenseMapper.MAPPING;
***
List<License> myList = MAPPING.jsonToDao(mySource);
U can do this :你可以这样做:
@Mapping(target = "yourTarget", source = "yourClass.custField.value") @Mapping(target = "yourTarget", source = "yourClass.custField.value")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.