jackson 序列化 返回值为map类型时,key为Object的情况

在feign中,返回值类型为Map< ObjectA, ObjectB>的情况时,发送jackson序列化失败,导致服务熔断,错误日志信息为

Can not find a (Map) Key deserializer for type

接口如下:

ResponseEntity<Map<EntityDto,List<XwbqDto>> mutipleGetLabels(@RequestBody List<EntityDto> entityDtos);

dto如下:

Data
public class EntityDto implements Serializable {

  private static final long SERIAL_VERSION_UID = -4680948320252253717L;

  private String entityId;

  private String entityType;

  public EntityDto() {

  }


  @Override
  public int hashCode() {
    int hashCode = (entityId + "-" + entityType).hashCode();
    return hashCode;
  }

  @Override
  public boolean equals(final Object obj) {
    if (obj == null) {
      return false;
    }
    final EntityDto entityDto = (EntityDto) obj;
    if (this == entityDto) {
      return true;
    } else {
      if (entityId==null&&entityType==null) {
        if (entityId == entityDto.getEntityId() && entityType == entityDto.getEntityType()) {
          return true;
        }
      }
      if (entityId==null&&entityType!=null){
        if (entityId== entityDto.getEntityId()&&entityType.equals(entityDto.getEntityType())){
          return true;
        }
      }
      if (entityId!=null&&entityType==null){
        if (entityId.equals(entityDto.getEntityId())&&entityType==entityDto.getEntityType()){
          return true;
        }
      }
      if (entityId!=null&&entityType!=null){
        if (entityId.equals(entityDto.getEntityId())&&entityType.equals(entityDto.getEntityType())){
          return true;
        }
      }
      return false;
    }
  }

}

在接口正确拿到了ResponseEntity<Map<EntityDto,List<XwbqDto>>对应的返回结果后,feign会利用jackson对结果进行json转换,此时会发生序列化及反序列化失败,导致服务调用发生异常;

解决办法:修改Dto,添加序列化及反序列化规则

1.序列化:重载Dto的toString方法,并增加 @JsonValue注解

2.反序列化:重载Dto的构造器,与toString中的内容正好相反

修改后的Dto代码如下

@Data
public class EntityDto implements Serializable {

  private static final long SERIAL_VERSION_UID = -4680948320252253717L;

  private String entityId;

  private String entityType;

  public EntityDto() {

  }

  public EntityDto(String obInfo) {
    String[] values=obInfo.split("-");
    this.entityId=values[0];
    this.entityType=values[1];
  }

  @Override
  @JsonValue
  public String toString(){
    return this.entityId + "-" + this.entityType;
  }

  @Override
  public int hashCode() {
    int hashCode = (entityId + "-" + entityType).hashCode();
    return hashCode;
  }

  @Override
  public boolean equals(final Object obj) {
    if (obj == null) {
      return false;
    }
    final EntityDto entityDto = (EntityDto) obj;
    if (this == entityDto) {
      return true;
    } else {
      if (entityId==null&&entityType==null) {
        if (entityId == entityDto.getEntityId() && entityType == entityDto.getEntityType()) {
          return true;
        }
      }
      if (entityId==null&&entityType!=null){
        if (entityId== entityDto.getEntityId()&&entityType.equals(entityDto.getEntityType())){
          return true;
        }
      }
      if (entityId!=null&&entityType==null){
        if (entityId.equals(entityDto.getEntityId())&&entityType==entityDto.getEntityType()){
          return true;
        }
      }
      if (entityId!=null&&entityType!=null){
        if (entityId.equals(entityDto.getEntityId())&&entityType.equals(entityDto.getEntityType())){
          return true;
        }
      }
      return false;
    }
  }
}

详细说明请参考https://www.baeldung.com/jackson-map

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值