尝试构建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"
}
}
1条答案
按热度按时间insrf1ej1#
我觉得你的绘图仪可能是反的。
与JSON结构直接相关的类是
Quote
。该错误使其看起来好像您正在尝试替代
QuoteFromSource
。另外,你需要确保你的类有标准/默认的getter和setter,因为默认情况下,Jackson会在编译时使用它们,除非你将其配置为直接进行属性注入(非默认)。
范例: