gpt4 book ai didi

java - 使用 mapstruct : Error:(22, 映射 48) java: 参数类型 "quote"没有名为 "quote_type"的属性

转载 作者:行者123 更新时间:2023-12-05 07:11:36 25 4
gpt4 key购买 nike

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

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(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"
}
}

最佳答案

我认为您的映射器可能落后了。

与您的 JSON 结构直接相关的类是 Quote

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

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

例子:

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

关于java - 使用 mapstruct : Error:(22, 映射 48) java: 参数类型 "quote"没有名为 "quote_type"的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60746717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com