今天自己测试了一下,往redis中存数据,代码没有报错,为什么redis中key乱码且看不到值,求解


package com.service;

public interface StudentService {

    void put(String key, String value);
}
package com.service.impl;

import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void put(String key, String value) {

        redisTemplate.opsForValue().set(key,value);
    }
}
package com.web;

import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/put")
    @ResponseBody
    public Object put(String key,String value){

        studentService.put(key,value);
        return "值已成功放入redis";
    }

}

img

你这个key和value是不是前端传的,你试一下在代码中写死key和value,看下存进去会不会变成乱码,如果不会,说明是前后端传值造成的,如果不会,再排查原因

乱码?设置下编码吧,网上搜下有很多方法

直接在控制台打印出前端传进来的值

你打印下值看前端传回来的值是否已经乱码了,如果没乱码的话。要看你的RedisTemplate有没配置序列化,RedisTemplate会出现编码问题,需要自行配置的。