jackson 使用mapstructMap:错误:(22,48)java:参数“quote”的类型没有名为“quote_type”的属性

g6ll5ycj  于 7个月前  发布在  Java
关注(0)|答案(1)|浏览(86)

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

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.

Map器类如下所示

@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"
    } 
}
insrf1ej

insrf1ej1#

我觉得你的绘图仪可能是反的。
与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;
    }
}

相关问题