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;
fatherazrael
  • 5,511
  • 16
  • 71
  • 155

2 Answers2

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); 
Nick
  • 805
  • 5
  • 14
  • Thanks for response but It is saying -> Unknown property "value" in result type java.util.List. Did you mean "empty"? This value exists in List inside License class... How to mention that in expression – fatherazrael Dec 31 '19 at 04:28
  • oh sorry. Try to replace String.valueOf(value) to String.valueOf(source.getValue() ) – Nick Dec 31 '19 at 04:30
  • Let me try. But getValue is not present in License. It is present in License.customFields.value. and customFields is of type list – fatherazrael Dec 31 '19 at 04:31
  • Tried this -> @Mapping(expression = "java( String.valueOf(source.customFields[].getValue()))", target = "value") but not able to fix. – fatherazrael Dec 31 '19 at 04:34
  • Oh sorry i did a mistake – Nick Dec 31 '19 at 07:26
  • 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? – fatherazrael Dec 31 '19 at 09:17
  • 1
    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 myList = MAPPING.jsonToDao(mySource); I will add this to code – Nick Dec 31 '19 at 09:21
  • Thanks Nick. Marked it as answer – fatherazrael Dec 31 '19 at 10:06
0

U can do this :

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

enter image description here

Sanych369
  • 1
  • 2