fat*_*ael 5 java dto mapper gson mapstruct
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);
}
Run Code Online (Sandbox Code Playgroud)
vo.license 包含具有以下属性的 CustomFields 列表
@SerializedName("Value")
@Expose
private Object value;
Run Code Online (Sandbox Code Playgroud)
Json 将一个字段作为对象输入,因为它可能是布尔值、字符串或任何其他内容,因此我已将其映射到对象中。而在 dao 层中,String 中有相同的字段。(在自定义映射器中,我只是 String.valueof 但不确定如何使用 Mapstrut 实现它)
谁能告诉我 LicenseMapper 中需要进行哪些设置才能将对象转换为字符串?
许可证结构 - 来源和目的地:
.
.
private String notes;
private Boolean isIncomplete;
private List<CustomField> customFields = null;
private List<Allocation> allocations = null;
Run Code Online (Sandbox Code Playgroud)
源中的自定义字段结构(删除了 gson 注释):
.
.
private String name;
private Object dataType;
private Object value;
Run Code Online (Sandbox Code Playgroud)
目的地中的自定义字段结构
private String name;
private String datatype;
private String value;
Run Code Online (Sandbox Code Playgroud)
您可以尝试将注释@Mapping与表达式一起使用
@Mapping(expression = "java( String.valueOf(source.getValue()) )", target = "value")
List<License> jsonToDao(List<com.integrator.vo.license.License> source);
Run Code Online (Sandbox Code Playgroud)
更新
@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);
}
Run Code Online (Sandbox Code Playgroud)
在你的代码中
import static ***.LicenseMapper.MAPPING;
***
List<License> myList = MAPPING.jsonToDao(mySource);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21621 次 |
| 最近记录: |