jackson序列化抽象类 || jackson.databind.exc.InvalidDefinitionException

问题描述

通常在微服务中需要调用远程服务,假如服务A 调用服务B的接口,而服务B的接口中需要的参数是一个 List<抽象类>,由于List中保存的抽象类没有明确指向具体的子类或实现类,因此序列化会失败。

报错信息:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of xxx.xxx.xxx.Product (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

服务B的接口

    @ApiOperation(value = "计算产品价格")
    @PostMapping("/calculatePrice")
    Result<?> calculatePrice(@RequestBody List<Product> products);

抽象类 Product

@Data
public abstract class Product{
    /**
     * 产品名称
     */
    private String name;
    /**
     * 产品类型
     */
    private Integer type;
    /**
     * 产品价格
     */
    private Long price;
}

实现类A

@Data
public class ProductA extends Product {
    /**
     * 产品规格
     */
    private long spec;
    /**
     * 订单编号
     */
    private String orderNo;
 }

实现类B

@Data
public class ProductB extends Product {
    /**
     * 订单编号
     */
    private String orderNo;
}

解决方案:

使用com.fasterxml.jackson.core的属性来解决上面问题

  • @JsonSubTypes – indicates sub-types of the annotated type 指出被注解类型的子类
  • @JsonTypeName – defines a logical type name to use for annotated class 定义被注解类使用的逻辑名称

1. 导入jackson坐标

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.5</version>
        </dependency>

2. 改造抽象类及其实现类

抽象类 Product

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({
        @JsonSubTypes.Type(value = ProductA.class, name = "A"),
        @JsonSubTypes.Type(value = ProductB.class, name = "B"),
})
public abstract class Product{
    /**
     * 产品名称
     */
    private String name;
    /**
     * 产品类型
     */
    private Integer type;
    /**
     * 产品价格
     */
    private Long price;
}

实现类A

@Data
@NoArgsConstructor
@JsonTypeName(value = "A")
public class ProductA extends Product {
    /**
     * 产品规格
     */
    private long spec;
    /**
     * 订单编号
     */
    private String orderNo;
 }

实现类B

@Data
@NoArgsConstructor
@JsonTypeName(value = "B")
public class ProductB extends Product {
    /**
     * 订单编号
     */
    private String orderNo;
}
2025-03-17 16:29:01.752 ERROR [] [http-nio-19008-exec-1] o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.alibaba.fastjson.JSON]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.alibaba.fastjson.JSON` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (PushbackInputStream); line: 1, column: 1]] with root cause com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.alibaba.fastjson.JSON` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (PushbackInputStream); line: 1, column: 1] at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1452) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1028) at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)这什么错误
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值