1
votes

I am using mapstruct to map from one DTO to another. I have multiple default methods , but 2 of them with a return value of String and that uses the same class as the input parameter gives me "Ambiguous mapping methods using java Mapstruct" error.


public class Action implements Serializable {

    [...]

    private String statusHistory;

    [...]

    private String propertiesOriginal;
    private String propertiesEdited;

    // Setter, Getter, ecc..
}

public class ActionDTO implements Serializable {

    ...
    private Map<String, Integer> statusHistory  = new HashMap<>();

    ...
    private Object propertiesOriginal;
    private Object propertiesEdited;

    // Setter, Getter, ecc..
}

@Mapper(componentModel = "spring", uses = {})
public interface ActionMapper extends EntityMapper<ActionDTO, Action> {

    default Map toMap(String text) throws IOException {
        Map map = new HashMap();
        try{
            map = new ObjectMapper().readValue(text, new TypeReference<Map<String, Integer>>(){});
        } catch (IOException e){
            e.printStackTrace();
        }
        return map;
    }

    default String fromMap(Map map){
        return new JSONObject(map).toString();
    }

[...]

    default Object toObject(String text) throws IOException {
        return new ObjectMapper().readValue(text, Object.class);
    }

    default String fromObject(Object object) throws IOException {
        return new ObjectMapper().convertValue(object, String.class);
    }
[...]
}

Error:(16, 7) java: Ambiguous mapping methods found for mapping property "java.lang.String statusHistory" to java.util.Map: java.util.Map toMap(java.lang.String text), java.lang.Object toObject(java.lang.String text).

1

1 Answers

0
votes

Both methods qualify Object and Map. You can however select the proper result type to use. Have a look at @BeanMapping#resultType.