【问题标题】:How to deserialize ResponseEntity for Redis?如何反序列化 Redis 的 ResponseEntity?
【发布时间】:2017-10-16 22:26:51
【问题描述】:

我正在我的组织中创建自定义缓存架构,并且必须序列化 ResponseEntity 对象。我正在使用 GenericJackson2JsonRedisSerializer 来序列化对象,但是当它是 ResponseEntity 时它不接受。

我使用 Jackson 序列化器的自定义 redis 模板:

 @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new CustomRedisTemplate<String, Object>();
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }

我的端点返回 ResponseEntity:

@RequestMapping(value = "/foo")
public ResponseEntity<String> foo() {
    return new ResponseEntity<>("bar", HttpStatus.OK);
}

错误日志:

Could not read JSON: Can not construct instance of org.springframework.http.ResponseEntity: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: [B@74297a01; line: 1, column: 137] (through reference chain: br.com.stilingue.cloudutilities.cache.CacheItem["value"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.http.ResponseEntity: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: [B@74297a01; line: 1, column: 137] (through reference chain: br.com.stilingue.cloudutilities.cache.CacheItem["value"])

在反序列化对象时出现问题,有谁知道我该怎么办?

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?
  • 找到解决方案了吗?
  • 很遗憾没有:/

标签: spring-boot redis spring-data-redis


【解决方案1】:

尝试在返回语句中添加类型信息:

return new ResponseEntity<String>("bar", HttpStatus.OK);

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 2018-10-06
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2021-01-26
    • 2018-09-25
    相关资源
    最近更新 更多