开发者社区> 问答> 正文

开启autoType,反序列化springSecurity的类,字段都为null了,求解

询问有关本项目的使用和其他方面的相关问题。 开启autoType,反序列化springSecurity的类,字段都为null了

提问7.png

提问8.png

我把RegisteredClient这个类缓存到redis里面,然后反序列化获取的时候,里面的值就为null了,下面是FastJson2JsonRedisSerializer的配置

提问9.png

我写一个demo复现下,不知道这个算不算bug

@Getter @ToString public class B implements Serializable {

private String username;
private int age;
protected B(){}
public static class Builder implements Serializable {
    private String username;
    private int age;
    public Builder username(String username) {
        this.username = username;
        return this;
    }
    public Builder age(int age) {
        this.age = age;
        return this;
    }
    public B build(){
        B b = new B();
        b.age = this.age;
        b.username = this.username;
        return b;
    }
}

}

public byte[] serialize(Object o) throws SerializationException {
    if (o == null) {
        return new byte[0];
    }
    return JSON.toJSONString(o, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
}
public Object deserialize(byte[] bytes) throws SerializationException {
    if (bytes == null || bytes.length <= 0) {
        return null;
    }
    String str = new String(bytes, StandardCharsets.UTF_8);
    return JSON.parseObject(str, Object.class, autoTypeFilter, JSONReader.Feature.SupportAutoType);
}
@Test
public void test6(){
    B.Builder build = new B.Builder();
    B b = build.age(123).username("yh").build();
    byte[] bytes2 = serialize(b);
    Object deserialize = deserialize(bytes2);
    System.out.println("-------"+deserialize);
}

执行结果: -------B(username=null, age=0)

原提问者GitHub用户yao-huan

展开
收起
白夜行fighting 2023-04-21 10:45:51 189 0
1 条回答
写回答
取消 提交回答
  • @Getter @ToString @JSONType(builder = B.Builder.class) public static class B implements Serializable { private String username; private int age;

        protected B() {
        }
    
        public static class Builder implements Serializable {
            private String username;
            private int age;
    
            public Builder withUsername(String username) {
                this.username = username;
                return this;
            }
    
            @JSONField
            public Builder withAge(int age) {
                this.age = age;
                return this;
            }
    
            public B build() {
                B b = new B();
                b.age = this.age;
                b.username = this.username;
                return b;
            }
        }
    }
    

    通过@jsontype指定Builder类,然后使用withUserName和withAge这样的名字。

    其中的serialize和deserialize方法可以如下的方式优化,可以减少中间转换提升性能:

    public byte[] serialize(Object o) throws SerializationException {
        if (o == null) {
            return new byte[0];
        }
        return JSON.toJSONBytes(o, JSONWriter.Feature.WriteClassName);
    }
    
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        return JSON.parseObject(bytes, Object.class, autoTypeFilter, JSONReader.Feature.SupportAutoType);
    }
    

    原回答者GitHub用户wenshao

    2023-04-21 13:43:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载