zoukankan      html  css  js  c++  java
  • SpringBoot中Redis配置类报错处理Unexpected_token(START_OBJECT)

    一、redisTemplate获取时报错:

    Caused by: com.fasterxml.jackson.databind.JsonMappingException:
    Unexpected token (START_OBJECT), expected START_ARRAY:
    need JSON Array to contain As.WRAPPER_ARRAY type information for class java.lang.Object

    二、报错代码行:

    Object obj = redisTemplate.opsForValue().get(key);
    

    三、找到配置类,注释掉这一行:

    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 
    

    完整的配置

    @Bean
    	public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
    		log.info(" --- redis config init --- ");
    		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =jacksonSerializer();
    		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    		redisTemplate.setConnectionFactory(lettuceConnectionFactory);
    		RedisSerializer<?> stringSerializer = new StringRedisSerializer();
    		// key序列化
    		redisTemplate.setKeySerializer(stringSerializer);
    		// value序列化
    		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
    		// Hash key序列化
    		redisTemplate.setHashKeySerializer(stringSerializer);
    		// Hash value序列化
    		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    		redisTemplate.afterPropertiesSet();
    		return redisTemplate;
    	}
    
    	private Jackson2JsonRedisSerializer jacksonSerializer() {
    		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    		ObjectMapper objectMapper = new ObjectMapper();
    		objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
    		// 保留这行会报错:Unexpected token (VALUE_STRING)
    		//objectMapper.enableDefaultTyping(DefaultTyping.NON_FINAL);
    		jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    		return jackson2JsonRedisSerializer;
    	}
    
  • 相关阅读:
    node js的模块
    前端学习ES6
    产品相关
    linux命令
    【jmeter】对于返回结果中文显示?问题
    MAC 本用pip3命令安装openpyxl插件(或者其他插件)后,在Pycharm依然找不到
    pip3版本已经是最新,安装openxl失败,Could not find a version that satisfies the requirement openxl
    MAC本安装python3.8后,pip3命令无法更新问题
    mac安装python环境
    nodejs安装步骤
  • 原文地址:https://www.cnblogs.com/eternityz/p/15329988.html
Copyright © 2011-2022 走看看