fatherazrael
fatherazrael

Reputation: 5977

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

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;

Upvotes: 5

Views: 25194

Answers (2)

Sanych369
Sanych369

Reputation: 1

U can do this :

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

enter image description here

Upvotes: 0

Nick
Nick

Reputation: 825

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

Upvotes: 6

Related Questions