【问题标题】:MapStruct: How to map property from "java.lang.Object to "java.lang.String"MapStruct:如何将属性从“java.lang.Object”映射到“java.lang.String”
【发布时间】:2019-12-31 04:16:51
【问题描述】:

MapStrut 新手;对象到字符串错误:

[错误] /util/LicenseMapper.java:[11,23] 无法将属性“java.lang.Object license.customFields[].value”映射到“java.lang.String license.customFields[]。价值”。考虑声明/实现一个映射方法:“java.lang.String map(java.lang.Object value)”。

代码:

@Mapper
public interface LicenseMapper {
    List<License> jsonToDao(List<com.integrator.vo.license.License> source);
}

vo.license 包含具有属性 as 的 CustomFields 列表

@SerializedName("Value")
@Expose
private Object value;

Json 将一个字段的输入作为对象,因为它可能是布尔值或字符串或任何值,因此我已将其映射到对象中。而在 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;

【问题讨论】:

    标签: java gson dto mapstruct mapper


    【解决方案1】:

    你可以尝试使用注解@Mapping with expression

    @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); 
    

    【讨论】:

    • 感谢您的回复,但它说 -> 结果类型 java.util.List 中的未知属性“值”。您指的是 “empty” 吗?该值存在于 License 类中的 List 中...如何在表达式中提及
    • 对不起。尝试将 String.valueOf(value) 替换为 String.valueOf(source.getValue() )
    • 让我试试。但是 getValue 在许可证中不存在。它存在于 License.customFields.value 中。并且 customFields 是列表类型
    • 试过这个 -> @Mapping(expression = "java( String.valueOf(source.customFields[].getValue()))", target = "value") 但无法修复。
    • 我更喜欢像这样在映射器中创建字段:“LicenseMapper MAPPING = Mappers.getMapper(LicenseMapper.class);”这允许我导入静态方法 import static ***.LicenseMapper.MAPPING 然后我可以使用这样的方法: List myList = MAPPING.jsonToDao(mySource);我会将其添加到代码中
    【解决方案2】:

    你可以这样做:

    @Mapping(target = "yourTarget", source = "yourClass.custField.value")

    enter image description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2019-07-04
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      相关资源
      最近更新 更多