Spring Boot(六)集成Spring session redis


项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot(六)集成Spring session redis ,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.6


Centos7 安装 Redis 3.2.0

http://blog.csdn.NET/a286352250/article/details/51603189


Centos7 开机启动 Redis 3.2.0

http://blog.csdn.Net/a286352250/article/details/52595387


Spring Boot(五)集成 Redis
http://blog.csdn.net/a286352250/article/details/53157201



pom.xml

<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>


web.xml集成 Spring Session Redis

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Archetype Created Web Application</display-name>
    
    <!-- delegatingFilterProxy -->  
    <filter>  
        <filter-name>springSessionRepositoryFilter</filter-name>  
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
    </filter>  
    <filter-mapping>  
        <filter-name>springSessionRepositoryFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
</web-app>


测试Session:

src/main/java/com/training/redis/controller/DemoRedisController.java

package com.training.redis.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.training.core.controller.BaseRedisController;
import com.training.core.dto.Pair;
import com.training.core.dto.ResultDataDto;
import com.training.redis.dto.DemoSessionDto;
import com.training.redis.entity.DemoRedis;
import com.training.redis.service.DemoRedisService;

@RestController
@RequestMapping(value="/demoRedis") 
public class DemoRedisController extends BaseRedisController<DemoRedis> {

	@Resource
	private DemoRedisService demoRedisService;
	
	/**
	 * 查询所有Session
	 */
	@ApiOperation(value="查询所有Session", notes="getEntityById")
    @RequestMapping(value = "/getSesstion", method = RequestMethod.GET)
	public @ResponseBody ResultDataDto getEntityById(HttpSession httpSession) {
    	
    	Enumeration<String> sessionkeys = httpSession.getAttributeNames();
    	
    	List<Pair<String, String>> list = new ArrayList<Pair<String,String>>();
    	while (sessionkeys.hasMoreElements()) {
			String key = (String) sessionkeys.nextElement();
			list.add(new Pair<String, String>(key, httpSession.getAttribute(key).toString()));
		}
		return ResultDataDto.addSuccess().setDatas(list);
	}
    
    /**
	 * 新增测试Session
	 */
	@ApiOperation(value="新增测试Session", notes="getEntityById")
    @RequestMapping(value = "/addSesstion", method = RequestMethod.GET)
	public @ResponseBody ResultDataDto addEntity(HttpSession httpSession) {
    	DemoSessionDto session = new DemoSessionDto();
    	session.setKey("testKey");
    	session.setValue("testValue");
    	httpSession.setAttribute(session.getKey(), session.getValue());
		return ResultDataDto.addAddSuccess();
	}
    
}


测试:

http://localhost:8080/addSesstion

重启 Tomcat

http://localhost:8080/getSesstion

Session依旧存在












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值