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



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

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

源类在下方给出

public class Quote {
@JsonProperty("quote_type")
private String type;
@JsonProperty("quote_value")
private Value value;
}

目的地类别在下方

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在反序列化时会使用这些getter和seter,除非您将其配置为直接(而非默认(进行属性注入

示例:

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

最新更新