Springboot项目中使用redis的配置详解
作者:Apollo的小太阳 发布时间:2021-11-26 03:43:44
标签:Springboot,redis,配置
程序结构:
一、配置
1. 在pom.xml中添加依赖
pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lyy</groupId>
<artifactId>redis-test</artifactId>
<version>0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<!--始终从仓库中获取-->
<!--<relativePath/>-->
</parent>
<dependencies>
<!--web应用基本环境,如mvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--redis包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
其中,spring-boot-starter-web包含springmvc。
2. 配置application.yml
application.yml文件如下:
server:
port: 11011
servlet:
context-path: /api/v1
spring:
redis:
# Redis数据库索引(默认为0)
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
# password: 123456
3. 通过配置类,设置redis
RedisConfig类如下:
package com.apollo.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author :apollo
* @since :Created in 2019/2/22
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Autowired
private ObjectMapper objectMapper;
/**
* 自定义springSessionDefaultRedisSerializer对象,将会替代默认的SESSION序列化对象。
* 默认是JdkSerializationRedisSerializer,缺点是需要类实现Serializable接口。
* 并且在反序列化时如果异常会抛出SerializationException异常,
* 而SessionRepositoryFilter又没有处理异常,故如果序列化异常时就会导致请求异常
*/
@Bean(name = "springSessionDefaultRedisSerializer")
public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
/**
* JacksonJsonRedisSerializer和GenericJackson2JsonRedisSerializer的区别:
* GenericJackson2JsonRedisSerializer在json中加入@class属性,类的全路径包名,方便反系列化。
* JacksonJsonRedisSerializer如果存放了List则在反系列化的时候,
* 如果没指定TypeReference则会报错java.util.LinkedHashMap cannot be cast。
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
new Jackson2JsonRedisSerializer(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
二、逻辑代码
1. 程序入口
package com.apollo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author :apollo
* @since :Created in 2019/2/22
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2. 实体类
实体类Animal如下:
package com.apollo.bean;
/**
* @author :apollo
* @since :Created in 2019/2/22
*/
public class Animal {
private Integer weight;
private Integer height;
private String name;
public Animal(Integer weight, Integer height, String name) {
this.weight = weight;
this.height = height;
this.name = name;
}
……这里是get、set方法
}
3. 公共返回类
package com.apollo.common;
/**
* @author :apollo
* @since :Created in 2019/2/22
*/
public class ApiResult {
public static final Integer STATUS_SUCCESS = 0;
public static final Integer STATUS_FAILURE = -1;
public static final String DESC_SUCCESS = "操作成功";
public static final String DESC_FAILURE = "操作失败";
private Integer status;
private String desc;
private Object result;
private ApiResult() {}
private ApiResult(Integer status, String desc, Object result) {
this.status = status;
this.desc = desc;
this.result = result;
}
//这个方法和Builder设计模式二选一即可,功能是重复的
public static ApiResult success(Object result) {
return success(DESC_SUCCESS, result);
}
//同上
public static ApiResult success(String desc, Object result) {
return new ApiResult(STATUS_SUCCESS, desc, result);
}
//同上
public static ApiResult failure(Integer status) {
return failure(status, null);
}
//同上
public static ApiResult failure(Integer status, String desc) {
return failure(status, desc, null);
}
//同上
public static ApiResult failure(Integer status, String desc, Object result) {
return new ApiResult(status, desc, result);
}
public static Builder builder() {
return new Builder();
}
//静态内部类,这里使用Builder设计模式
public static class Builder {
private Integer status;
private String desc;
private Object result;
public Builder status(Integer status) {
this.status = status;
return this;
}
public Builder desc(String desc) {
this.desc = desc;
return this;
}
public Builder result(Object result) {
this.result = result;
return this;
}
public ApiResult build() {
return new ApiResult(status, desc, result);
}
}
……这里是get、set方法,这里的方法一定不能少,否则返回时无法将对象序列化
}
4. 请求处理Controller
RedisController类如下:
package com.apollo.controller;
import com.apollo.bean.Animal;
import com.apollo.common.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author :apollo
* @since :Created in 2019/2/22
*/
@RestController
@RequestMapping(value = "/redis")
public class RedisController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 测试向redis中添加数据
* @param id
* @return
*/
@GetMapping(value = "/{id}")
public ApiResult addData2Redis(@PathVariable("id") Integer id) {
redisTemplate.opsForValue().set("first", id);
redisTemplate.opsForValue().set("second", "hello world");
redisTemplate.opsForValue().set("third",
new Animal(100, 200, "二狗子"));
return ApiResult.builder()
.status(ApiResult.STATUS_SUCCESS)
.desc("添加成功")
.build();
}
/**
* 测试从redis中获取数据
* @return
*/
@GetMapping("/redis-data")
public ApiResult getRedisData() {
Map<String, Object> result = new HashMap<>();
result.put("first", redisTemplate.opsForValue().get("first"));
result.put("second", redisTemplate.opsForValue().get("second"));
result.put("third", redisTemplate.opsForValue().get("third"));
return ApiResult.builder()
.status(ApiResult.STATUS_SUCCESS)
.desc("获取成功")
.result(result)
.build();
}
}
注意:这里是返回ApiResult对象,需要将返回的对象序列化,所以ApiResult中的get/set方法是必须的,否则会报错:HttpMessageNotWritableException: No converter found for return value of type: class com.apollo.common.ApiResult,找不到ApiResult类型的转换器。
三、测试
1. 测试添加
使用postman请求http://localhost:11011/api/v1/redis/5,返回结果:
{
"status": 0,
"desc": "添加成功",
"result": null
}
登录到redis,使用命令dbsize查看存储的数据量:
数据量为3,对应我们上边程序中的3步操作。
2. 测试获取
使用postman请求http://localhost:11011/api/v1/redis/redis-data,返回结果:
{
"status": 0,
"desc": "获取成功",
"result": {
"third": {
"weight": 100,
"height": 200,
"name": "二狗子"
},
"first": 5,
"second": "hello world"
}
}
与我们之前存入的数据对比,是正确的。
四、代码地址
github地址:https://github.com/myturn0/redis-test.git
来源:https://blog.csdn.net/wodeshouji6/article/details/87885087
0
投稿
猜你喜欢
- RabbitMQ作为AMQP的代表性产品,在项目中大量使用。结合现在主流的spring boot,极大简化了开发过程中所涉及到的消息通信问题
- 昨天实现一个功能,根据文章的id或者别名查找文章。起初采用mybatis的Example进行查询,对参数artName进行判断,如果是纯数字
- FeignClient脱离eureka自定义URL需求Spring Cloud环境中的FeignClient有时候需要调用特定主机的接口,但
- 我们主要介绍一下:java中如何通过最简单的方式实现链式创建json对象,解决创建json代码臃肿的问题。1、假设我们要创建一个json对象
- Java常用API介绍API概念什么是API?API(Application Programming interface) 应用程序编程接口
- 动态内存管理为什么存在动态内存分配我们到现在为止掌握的是什么样的内存开辟方式呢//创建一个变量int val = 20; &n
- 用途项目中使用了 dubbo,注册中心使用的 zookeeper,使用 zookeeper 实现了一个简单的分布式锁(依赖 curator)
- 1、动态SQL片段通过SQL片段达到代码复用 <!-- 动态条件分页查询 --> <sql i
- 1.新建文件上传页面在static目录中新建upload-test.html,上传页面代码如下所示:<!DOCTYPE html>
- 1、什么是servlet异步请求Servlet 3.0 之前,一个普通 Servlet 的主要工作流程大致如下:(1)、Servlet 接收
- 本文实例为大家分享了Java从服务端下载Excel模板文件的具体实现代码,供大家参考,具体内容如下方法一 (2021年01月更新)生成exc
- 最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文
- 1、数组数组的引用传递public class TestDemo1{public static void main(String args[
- 在spring中有很多以XXXAware命名的接口,很多人也不清楚这些接口都是做什么用的,这篇文章将描述常用的一些接口。一,Applicat
- 本文实例讲述了Java基于Runtime调用外部程序出现阻塞的解决方法, 是一个很实用的技巧。分享给大家供大家参考。具体分析如下:有时候在j
- 导语相信大家无论是做前端还是做后端的,都被接口接口文档所折磨过,前端抱怨接口文档和后端给的不一致,后端抱怨写接口文档很麻烦,所以Swagge
- 1. 什么是RESTREST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状
- 本文主要对SpringBoot2.x参数校验进行简单总结,其中SpringBoot使用的2.4.5版本。一、引入依赖<dependen
- mybatis-plus作为mybatis的增强工具,简化了开发中的数据库操作。一旦遇到left join或right join的左右连接,
- 前段时间摸索了java调用matlab东西,不说学的有多深,也算有结果了,达到目的了。也即用java程序可以调用matlab中函数了。&nb