MapStruct: How to map property from "java.lang.Object to "java.lang.String"
Asked Answered
L

2

5

New to 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". Consider to declare/implement a mapping method: "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

@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. Whereas in dao layer has same field in String. (In custom mapper i just String.valueof but not sure how to achieve it using Mapstrut)

Can anyone tell me what settings are required in LicenseMapper to convert Object to String?

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):

.
.
private String name;
private Object dataType;
private Object value;

Custom FIeld Structure in Destination

private String name;
private String datatype;
private String value;
Lorin answered 31/12, 2019 at 4:16 Comment(0)
K
6

You can try to use annotation @Mapping with expression

@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); 
Khajeh answered 31/12, 2019 at 4:25 Comment(8)
Thanks for response but It is saying -> Unknown property "value" in result type java.util.List<com.integrator.snow.model.license.License>. Did you mean "empty"? This value exists in List<CustomField> inside License class... How to mention that in expressionLorin
oh sorry. Try to replace String.valueOf(value) to String.valueOf(source.getValue() )Khajeh
Let me try. But getValue is not present in License. It is present in License.customFields.value. and customFields is of type listLorin
Tried this -> @Mapping(expression = "java( String.valueOf(source.customFields[].getValue()))", target = "value") but not able to fix.Lorin
Oh sorry i did a mistakeKhajeh
Great Man. It worked for me and even by your example i am ale to understand how things work as i was not able to get straightforward example for this scenario. Kindly remove snippet above that. One more thing if you can help. How to call this LicenseMapper in main code?Lorin
I prefer to create field in mapper like this : " LicenseMapper MAPPING = Mappers.getMapper(LicenseMapper.class); " This allow me to import static method import static ***.LicenseMapper.MAPPING and then i can use methods like that : List<License> myList = MAPPING.jsonToDao(mySource); I will add this to codeKhajeh
Thanks Nick. Marked it as answerLorin
H
0

U can do this :

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

enter image description here

Headboard answered 14/7, 2021 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.