欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot 2.x 不使用默认序列化,自定义配置使用Json序列化

程序员文章站 2022-05-01 15:54:13
...

SpringBoot 2.x 不使用默认的jdk序列化,自定义配置Json序列化

自定义配置在config类,前面的类都是为了测试演示转换json成功没,所以可以直接跳过.

首先创建一个实体类实现Serializable接口:

public class Employee implements Serializable {
    private Integer id;
    private String lastName;
    private String email;
    private Integer gender;
    private Integer dId;

    public Employee() {
        super();
    }

    public Employee(Integer id, String lastName, String email, Integer gender, Integer dId) {
        super();
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
        this.dId = dId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Integer getdId() {
        return dId;
    }

    public void setdId(Integer dId) {
        this.dId = dId;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", gender=" + gender +
                ", dId=" + dId +
                '}';
    }
}

mapper类:
(只写了一个简单的查询)

@Mapper
public interface DepartmentMapper {
    @Select("select * from department where id=#{id}" )
    public Department getDeptById(Integer id);
}

service类:

@Service
public class DeptService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Cacheable(value = "dept" )
    public Department getDeptById(Integer id){
        System.out.println("查询部门:"+id);
        Department department = departmentMapper.getDeptById(id);
        return department;
    }
}

controller类:

@RestController
public class DeptController {
    @Autowired
    public DeptService deptService;
    @GetMapping("/dept/{id}")
    public Department getDept(@PathVariable("id") Integer id){
       return deptService.getDeptById(id);
    }
}

config类配置RedisConfig:

@Configuration
public class MyRedisConfig {
    @Bean
    public RedisTemplate<Object, Object> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException {
        RedisTemplate<Object,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        template.setDefaultSerializer(serializer);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory){
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofDays(1))
                .disableCachingNullValues()
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build();}
}

配置好了config类,就可以使用 @Cacheable(value = “dept” )注解,数据会存入缓存中以json的形式存放进去,我的 @Cacheable(value = “dept” )注解使用在service里面

SpringBoot 2.x 不使用默认序列化,自定义配置使用Json序列化

相关标签: java