栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

20220530Java在Redis里用FastJson存取封装对象Object

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

20220530Java在Redis里用FastJson存取封装对象Object

Background

If I save the content as String type into Redis, the matter is that how I can save as Json format and how to get an Object.class when we take out the text content from Redis?

My friend told me that using FastJson to save Object into Redis,then take out then with the format of json:

  • DTO bean
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserLoginDTO {
    private String userName;
    private String password;
}
Example 1: JSON.parseObject

Here I use JSONObject.toJSONString(Object obj) to make an Object changed to JsonString,

then, to take out and transfer it into an Object type by using JSON.parseObject(@Nullable String text,Class claz).

Codes reference as below:

   @Test
    void test1(){
        stringRedisTemplate.opsForValue().set("user:1054", JSONObject.toJSONString(new UserLoginDTO("kiseki", "12345678")));
        String s = stringRedisTemplate.opsForValue().get("user:1054");
        System.out.println(s);
        UserLoginDTO userLoginDTO = JSON.parseObject(s, UserLoginDTO.class);
        System.out.println(userLoginDTO);
    }

//{"password":"12345678","userName":"kiseki"}
//UserLoginDTO(userName=kiseki, password=12345678)

Example 2:JSON.parseArray

If there is some needs requiring json to contain a list type.It should be used by JSON.parseObject(s @Nullable String text,Class claz).

Then we will use JSON.parseArray(@Nullable String text,Class claz) to transfer the json to a List.
Codes reference as below:

    @Test
    void test2() {
        List list = new ArrayList<>();
        list.add(new UserLoginDTO("root1", "1314520"));
        list.add(new UserLoginDTO("root2", "1314521"));
        list.add(new UserLoginDTO("root3", "5201314"));
        list.add(new UserLoginDTO("root4", "5211314"));
        System.out.println(list);
        //then we put the list  into redis with String Type by JSONObject.toJSONString()
        stringRedisTemplate.opsForValue().set("user:blackList:may", JSONObject.toJSONString(list));
        String s = stringRedisTemplate.opsForValue().get("user:blackList:may");
        System.out.println(s);
        //transfer into DTO lists
        List userLoginDTOS = JSON.parseArray(s, UserLoginDTO.class);
        System.out.println(userLoginDTOS);
    }

//System.out.println(list):
//[UserLoginDTO(userName=root1, password=1314520), UserLoginDTO(userName=root2, password=1314521), UserLoginDTO(userName=root3, password=5201314), UserLoginDTO(userName=root4, password=5211314)]


//System.out.println(s):
//[{"password":"1314520","userName":"root1"},{"password":"1314521","userName":"root2"},{"password":"5201314","userName":"root3"},{"password":"5211314","userName":"root4"}]


//System.out.println(userLoginDTOS):
//[UserLoginDTO(userName=root1, password=1314520), UserLoginDTO(userName=root2, password=1314521), UserLoginDTO(userName=root3, password=5201314), UserLoginDTO(userName=root4, password=5211314)]

//we can see that the content of userLoginDTOS is same as list's.

We can see that the content of userLoginDTOS is same as list’s through the comparison !

Note:Sometimes,there are some accidents with serialization and deserialization,especially in the situation of cross-language working space.It seems that it’s necessary to operate in this way~~~~

有些情况反序列化的时候会出问题,特别是跨语言!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号