Mapping using mapstruct : Error:(22,48) java: The type of parameter "quote" has no property named "quote_type"

Viewed 5553

Getting the below error when trying to build the mapper class.

Error:(20,48) java: The type of parameter "quote" has no property named "quote_type".
Error:(15,53) java: Unknown property "quote_type" in return type.
Error:(20,48) java: Property "type" has no write accessor.

Mapper class is given below

@Mapper(componentModel = "spring")
public interface SourceDestinationMapper {

    @Mappings({
        @Mapping(target = "quote_type", source= "quoteFromSource.type")
    })
    Quote sourceToDestination(QuoteFromSource quoteFromSource);

    @Mappings({
        @Mapping(target = "type", source = "quote.quote_type")
    })
    QuoteFromSource destinationToSource(Quote quote);

    Value sourceValueToDestinationValue(ValueFromSource valueFromSource);

    ValueFromSource sourceValueToDestinationValue(Value value);
}

The Source class is given below

public class Quote {

    @JsonProperty("quote_type")
    private String type;

    @JsonProperty("quote_value")
    private Value value;
}

The Destination class is given below

public class QuoteFromSource {

    @JsonProperty("type")
    private String type;

    @JsonProperty("value")
    private ValueFromSource value;
}

source class

public class Value {

    @JsonProperty("quote_id")
    private Integer id;

    @JsonProperty("quote_description")
    private String quote;
}

destination class

public class ValueFromSource {

    @JsonProperty("id")
    private Integer id;

    @JsonProperty("quote")
    private String quote;
}

Example JSON to be deserialized:

{ 
    "quote_type": "auto",
    "quote_value": {
        "quote_id": 10,
        "quote_description": "This is my first quote"
    } 
}
1 Answers

I think your mapper might be backwards.

The class that directly correlates to your JSON structure is Quote.

The error makes it appear as if you are attempting to instead deserialize QuoteFromSource.

Additionally, you need to make sure your classes have standard / default getters and setters because by default, Jackson makes use of those when deserializing unless you configure it to do property injection directly (not default)

Example:

public class Quote {

    private String type;

    private Value value;

    @JsonSetter("quote_type")
    public void setType(String type) {
        this.type = type;
    }

    @JsonGetter("quote_type")
    public String getType() {
        return type;
    }

    @JsonSetter("quote_value")
    public void setValue(Value value) {
        this.value = value;
    }

    @JsonGetter("quote_value")
    public Value getValue() {
        return value;
    }
}
Related