SpringBoot 取出redis里面的数据,报错: org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unexpected character (',' (code 44)): Expected space separating root-level values

当已经利用Spark往Redis写入数据,这部分需自行查看资料,比较简单:

// 调用 sparkContext 实例化进行计算
val sc: SparkContext = {
  val sparkConf = new SparkConf()
    .setAppName(this.getClass.getSimpleName.stripSuffix("$"))
    .setMaster("local[3]")

    .set("spark.redis.host", "192.168.88.1xx") //redis 主机节点
    .set("spark.redis.port", "6379") //端口号,不填默认为6379
    .set("spark.redis.auth", "123456")

  new SparkContext(sparkConf)
}
sc.setLogLevel("WARN")
// 求data
val stEndRdd: RDD[(Int, Int)] = sc.makeRDD(stEndArr)

val allPathSeq: RDD[(String, String)] = stEndRdd.map(getGraphCalcu(_)(g))  // 函数操作


// 写入redis 
sc.toRedisKV(RddData)

写入数据后,key 和 value 均为字符串类型

 

 这时,如果通过 Spring Boot jquery 之 ajax 直接读取 redis 里面的数据

Object o = redisTemplate.opsForValue().get("1");
System.out.println(o);  

报异常:

org.springframework.data.redis.serializer.SerializationException: Could not read JSON: 
Unexpected character (',' (code 44)): Expected space separating root-level values
 at [Source: (byte[])"33,1232,1231,1230,1229,1228,1227,324,1438,1439,1440"; line: 1, column: 4]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character (',' (code 44)): Expected space separating root-level values   

可以看到数据出现了,但是没有读出来,初步估计是 RedisTemplate 出现了问题。

在SpringBoot 中注解 @Configuration 的模板部分出现了问题

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);

        // value序列化方式采用jackson,
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();
        return template;
    }
}

仅仅需要将  // value序列化方式采用jackson,  改成 stringRedisSerializer 类型就能读出来了。

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);

        // value序列化方式采用 String
        template.setValueSerializer(stringRedisSerializer);
        // hash的value序列化方式采用String
        template.setHashValueSerializer(stringRedisSerializer);


        template.afterPropertiesSet();
        return template;
    }
}

关于SpringBoot 如何直接读取Redis 里面的数据就不介绍了,网上有很多教程哪。

左侧有个推荐,有用就推荐下吧?☺

posted @ 2021-08-03 12:12  谦曰盛  阅读(7660)  评论(0编辑  收藏  举报