MapStrut的新功能;对象到字符串错误:
[错误] /util/LicenseMapper.java:[11,23]无法将属性“ java.lang.Object license.customFields []。value”映射到“ java.lang.String license.customFields []。value”。考虑声明/实现一个映射方法:“ java.lang.String map(java.lang.Object value)”。
码:
@Mapper
public interface LicenseMapper {
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
}
vo.license包含具有以下属性的CustomFields列表:
@SerializedName("Value")
@Expose
private Object value;
杰森有一个字段作为对象的输入,因为它可能会布尔或字符串或任何东西,所以我已经将其映射到对象。而dao层在String中具有相同的字段。(在自定义映射器中,我只是String.valueof,但不确定如何使用Mapstrut实现它)
谁能告诉我LicenseMapper中需要什么设置才能将Object转换为String?
许可证结构-源和目标:
.
.
private String notes;
private Boolean isIncomplete;
private List<CustomField> customFields = null;
private List<Allocation> allocations = null;
Source中的自定义字段结构(已删除gson注释):
.
.
private String name;
private Object dataType;
private Object value;
目标中的自定义字段结构
private String name;
private String datatype;
private String value;
您可以尝试对表达式使用注释@Mapping
@Mapping(expression = "java( String.valueOf(source.getValue()) )", target = "value")
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
更新
@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);
}
用您的代码
import static ***.LicenseMapper.MAPPING;
***
List<License> myList = MAPPING.jsonToDao(mySource);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句