使用mapstruct进行映射:错误:(22,48)java:参数“quote”的类型没有名为“quote_type”的属性

问题描述 投票:0回答:1

尝试构建映射器类时出现以下错误。

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 类如下所示

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

Source 类如下所示

public class Quote {

    @JsonProperty("quote_type")
    private String type;

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

Destination 类别如下

public class QuoteFromSource {

    @JsonProperty("type")
    private String type;

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

源类

public class Value {

    @JsonProperty("quote_id")
    private Integer id;

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

目的地舱位

public class ValueFromSource {

    @JsonProperty("id")
    private Integer id;

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

要反序列化的 JSON 示例:

{ 
    "quote_type": "auto",
    "quote_value": {
        "quote_id": 10,
        "quote_description": "This is my first quote"
    } 
}
java spring-boot jackson mapstruct
1个回答
0
投票

我认为你的映射器可能是落后的。

与 JSON 结构直接相关的类是

Quote

该错误看起来好像您正在尝试反序列化

QuoteFromSource

此外,您需要确保您的类具有标准/默认的 getter 和 setter,因为默认情况下,Jackson 在反序列化时会使用这些方法,除非您将其配置为直接进行属性注入(不是默认)

示例:

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.