1.jackson使用
//使用jackson保存所有的测试题到question.json文件中。
@Test
public void questionToJson() throws IOException {
List<Question> list = questionService.queryAll();
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.writeValue(new File("questionList.json"),list);
}
//使用jackson读取question.json,保存到list<question>.
@Test
public void JsonToQuestion() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Question.class);
List<Question> deptList = objectMapper.readValue(new File("questionList.json"), javaType);
for (Question question : deptList) {
System.out.println(question.toString());
}
}
2.redisTemplate使用
2.1 新建配置类RedisConfig
package com.yun.springboot_redis.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author zyk
* @version 1.0
* @fileName RedisConfig
* @description :TODO
* @date 2022/1/10 17:56
*/
@Configuration
public class RedisConfig {
@Bean(value = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
//使用jackson序列化工具(default JDK serialization)
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
//设置任何属性可见
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//序列化的时候将类名称序列化到json串中
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//使用redis自带的字符串序列化工具序列化key和value
RedisSerializer redisSerializer = new StringRedisSerializer();
//key
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
//value
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//初始化redisTemplate
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
2.2 添加配置
#redis服务器的IP地址
spring.redis.host=192.168.230.180
#redis的端口号
spring.redis.port=6379
pom.xml引入依赖
<!-- 引入redistempalte-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 引入jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>