询问有关本项目的使用和其他方面的相关问题。 开启autoType,反序列化springSecurity的类,字段都为null了
我把RegisteredClient这个类缓存到redis里面,然后反序列化获取的时候,里面的值就为null了,下面是FastJson2JsonRedisSerializer的配置
我写一个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
@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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。