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;
/**
* @Description:
* @author: Spike Wong
* @Created: 2022/5/15
*/
@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<UserLoginDTO> 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<UserLoginDTO> 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~~~~
有些情况反序列化的时候会出问题,特别是跨语言!