导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
yml文件配置redis
spring:
redis:
port: 6379
password: 123456
host: 127.0.0.1 #主机地址为服务器地址,此处使用本地
简单的String共享
第一个项目,用来传递信息
@RestController
@RequestMapping("/session")
public class SessionShareController {
@Autowired
private StringRedisTemplate redisTemplate;
//取yml中文件配置的端口号
@Value("${server.port}")
Integer port;
@GetMapping("/set")
public String set(HttpSession session){
session.setAttribute("name","张三");
redisTemplate.opsForValue().set("session",(String) session.getAttribute("name"));
return String.valueOf(port);
}
}
第二个项目,用来获取信息
@Autowired
private StringRedisTemplate redisTemplate;
//取yml中文件配置的端口号
@Value("${server.port}")
Integer port;
@GetMapping(value = "get")
public String get(HttpSession session){
return "用户:"+redisTemplate.opsForValue().get("session")+port;
}
传递信息接口测试
获取信息接口测试
由测试结果可以看出,能够获取到传递数据
传递的数据为对象时
我自己的思路是把对象转为json字符串,实质上还是通过字符串去传递信息(方法比较笨,但是容易理解)
第一个项目,用来传递信息
package com.llj.controller;
import com.alibaba.fastjson.JSON;
import com.llj.bean.User;
import com.llj.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
@RequestMapping(value = "/redis")
public class RedisObjectController {
@Autowired
private StringRedisTemplate redisTemplate;
@Value("${server.port}")
Integer port;
@GetMapping(value = "/set")
public String set(HttpSession session){
User user = new User("张三","123456");
//对象转json字符串
String userJson = JSON.toJSONString(user);
session.setAttribute("user",userJson);
redisTemplate.opsForValue().set("session",(String) session.getAttribute("user"));
return String.valueOf(port);
}
}
第二个项目用来,接收数据
package com.example.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
@RequestMapping("/redis")
public class RedisObjectController {
@Autowired
private StringRedisTemplate redisTemplate;
@Value("${server.port}")
Integer port;
@GetMapping("/get")
public String set(HttpSession session) {
//获取传递的字符串
String userJson = redisTemplate.opsForValue().get("session");
//json字符串转json对象
JSON json = JSON.parseObject(userJson);
//json对象转对象
User user = JSON.toJavaObject(json, User.class);
System.out.println(user.toString());
return "用户:" + userJson + port;
}
}
传递信息接口测试:
获取信息接口测试
经浏览器显示,以及控制台输出,此方法可行