当我们使用@Cacheable注解的时候会将返回的对象缓存起来,我们会发现默认缓存的值是二进制的,不方便查看,为此我们自定义序列化配置,改成JSON格式的

配置如下:

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.cjs.example</groupId>
  7. <artifactId>cjs-springsecurity-example</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>cjs-springsecurity-example</name>
  12. <description></description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.0.2.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-cache</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-data-redis</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-security</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-web</artifactId>
  47. </dependency>
  48.  
  49. <dependency>
  50. <groupId>org.projectlombok</groupId>
  51. <artifactId>lombok</artifactId>
  52. <optional>true</optional>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework.security</groupId>
  61. <artifactId>spring-security-test</artifactId>
  62. <scope>test</scope>
  63. </dependency>
  64. </dependencies>
  65.  
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.springframework.boot</groupId>
  70. <artifactId>spring-boot-maven-plugin</artifactId>
  71. </plugin>
  72. </plugins>
  73. </build>
  74.  
  75. </project>

application.yml

  1. spring:
  2. cache:
  3. type: redis
  4. redis:
  5. cache-null-values: false
  6. time-to-live: 3600000ms
  7. redis:
  8. host: 10.123.52.189
  9. port:
  10. database:
  11. password: 自己的密码
  12. logging:
  13. level:
  14. root: info

RedisConfig.java

  1. package com.cjs.example.config;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  4. import com.fasterxml.jackson.annotation.PropertyAccessor;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.cache.annotation.CachingConfigurerSupport;
  8. import org.springframework.cache.annotation.EnableCaching;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  12. import org.springframework.data.redis.cache.RedisCacheManager;
  13. import org.springframework.data.redis.cache.RedisCacheWriter;
  14. import org.springframework.data.redis.connection.RedisConnectionFactory;
  15. import org.springframework.data.redis.core.RedisTemplate;
  16. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  17. import org.springframework.data.redis.serializer.RedisSerializationContext;
  18. import org.springframework.data.redis.serializer.StringRedisSerializer;
  19.  
  20. @EnableCaching
  21. @Configuration
  22. public class RedisConfig extends CachingConfigurerSupport {
  23.  
  24. @Autowired
  25. private RedisConnectionFactory redisConnectionFactory;
  26.  
  27. @Bean
  28. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  29. Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  30. ObjectMapper objectMapper = new ObjectMapper();
  31. objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  32. objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  33. serializer.setObjectMapper(objectMapper);
  34.  
  35. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  36. redisTemplate.setConnectionFactory(redisConnectionFactory);
  37. redisTemplate.setKeySerializer(new StringRedisSerializer());
  38. redisTemplate.setValueSerializer(serializer);
  39. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  40. redisTemplate.setHashValueSerializer(serializer);
  41. redisTemplate.afterPropertiesSet();
  42.  
  43. return redisTemplate;
  44. }
  45.  
  46. @Bean
  47. public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
  48. RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
  49. RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
  50. .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
  51. return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
  52. }
  53.  
  54. /**
  55. * 二者选其一即可
  56. */
  57.  
  58. // @Bean
  59. // public RedisCacheConfiguration redisCacheConfiguration() {
  60. // Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  61. // ObjectMapper objectMapper = new ObjectMapper();
  62. // objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  63. // objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  64. // serializer.setObjectMapper(objectMapper);
  65. // return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
  66. // }
  67.  
  68. }

UserServiceImpl.java

  1. package com.cjs.example.service.impl;
  2.  
  3. import com.cjs.example.dao.UserDao;
  4. import com.cjs.example.entity.SysUser;
  5. import com.cjs.example.service.UserService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.cache.annotation.Cacheable;
  8. import org.springframework.stereotype.Service;
  9.  
  10. @Service
  11. public class UserServiceImpl implements UserService {
  12.  
  13. @Autowired
  14. private UserDao userDao;
  15.  
  16. @Cacheable(cacheNames = "authority", key = "#username")
  17. @Override
  18. public SysUser getUserByName(String username) {
  19. return userDao.selectByName(username);
  20. }
  21. }

反复看文档,一遍又一遍

最最重要的是

代码上传至https://github.com/chengjiansheng/cjs-springsecurity-example

Spring Boot Cache配置 序列化成JSON字符串的更多相关文章

  1. C#将对象序列化成JSON字符串

    C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...

  2. 使用 EntityFramework后把一个对象序列化成json字符串引起循环引用的问题

    先看一个T4模板生成的model实体类 著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:卷猫 链接:http://anneke.cn/ArticleInfo/Detial ...

  3. SpringMVC将表单对象序列化成Json字符串提交,以List接收

    出自:http://blog.csdn.net/m0_37595732/article/details/71440853 HTML <%@ page language="java&qu ...

  4. .net 将List序列化成Json字符串

    将List类型转化为Json,是我们平常开发时最常见的了.在使用中,有很多种方法,也可以使用. 第一种 第三方组件:Newtonsoft.Json.dll //转化成Json Newtonsoft.J ...

  5. 对象序列化成Json字符串 及 反序列化成对象

    一. public static string JsonSerializer<T>(T t)        {            DataContractJsonSerializer ...

  6. jackson2.8.4java对象序列化成json字符串格式化时间

    public class User {private int id; private Date birthday; private double money; private String name; ...

  7. 转:spring boot log4j2配置(使用log4j2.yml文件)---YAML 语言教程

    转:spring boot log4j2配置(使用log4j2.yml文件) - CSDN博客http://blog.csdn.net/ClementAD/article/details/514988 ...

  8. (36)Spring Boot Cache理论篇【从零开始学Spring Boot】

    Spring Boot Cache理论篇 在上一篇中我们介绍了Spring Boot集成Redis的实战例子,里面使用到了Spring Cache,那么什么是Spring Cache呢,本章将会做一个 ...

  9. 玩转spring boot——properties配置

    前言 在以往的java开发中,程序员最怕大量的配置,是因为配置一多就不好统一管理,经常出现找不到配置的情况.而项目中,从开发测试环境到生产环境,往往需要切换不同的配置,如测试数据库连接换成生产数据库连 ...

随机推荐

  1. 了解 ptyhon垃圾回收机制

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...

  2. ubuntu制作离线包

    一.应用场景a.当我们需要在多台电脑安装同一个软件,并且这个软件很大,下载需要很长时间b.需要安装软件的ubuntu不能上网二.离线安装包的制作2.1.通过如下指令下载XXXX软件所需要的deb包,首 ...

  3. 微信小程序支付遇到的坑

    1,微信公众号支付和微信小程序支付有差异 微信公众号:可以直接跳转走h5的微信支付 微信小程序:在测试环境.沙箱环境使用微信公众号的跳转支付没有问题,在线上存在支付异常 最后商讨的解决方法 openi ...

  4. wsl 子系统 用户目录位置

    C:\Users\DELL\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalS ...

  5. Open-Domain QA -paper

    Open-domain QA Overview The whole system is consisted with Document Retriever and Document Reader. T ...

  6. centos7安装tomcat8.5

    1.下载 tomcat Linux 版本 tomcat 官网下载地址:http://tomcat.apache.org/download-80.cgi 百度云盘链接:链接: https://pan.b ...

  7. mac上配置react-native环境run-ios/run-android命令遇到的问题

    新报错(rn版本:0.53.3)2018.3.6 今天在搞react-native环境时,遇到了一些坑,这里记录一下. 首先最重要的一点是一定要按官网一步一步来,不然可能会出现一些奇奇怪怪的问题! 官 ...

  8. Go语言基础之接口

    Go语言基础之接口 接口(interface)定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节. 接口 接口介绍 在Go语言中接口(interface)是一种类型,一种抽象的类 ...

  9. java课程课后作业190425之一维数组最大子数组(界面实现)

    题目要求: 1.在第一个问题过程中,我在以前的代码中好像已经写出了这个功能,想要实现这个功能,我们只需要在我们储存的数组和是负数的时候对中转值进行重新赋值就可以得到新的数值的起始位置,而他的终了位置就 ...

  10. C#线程的使用(1)

    今天刚开始学习使用线程,把学习过程与新的记录下来. 创建线程: 非常简单,只需声明她并为其提供线程起始点处的方法委托即可: 终止线程: 使用Abort和Join方法来实现: Abort方法:用于永久的 ...