springboot2.X 用redis+fastjson 报错 autoType is not support 已经setAutoTypeSupport=ture

2ul0zpep  于 4个月前  发布在  Spring
关注(0)|答案(8)|浏览(118)

设置代码如下

ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
log.info("fastjson isAutoType: {}",ParserConfig.getGlobalInstance().isAutoTypeSupport());

启动输出log如下

2018-09-04 21:59:35.225  INFO 11815 --- [           main] c.c.k.f.c.r.c.RedisConfiguration         : fastjson isAutoType: true

依旧报错

Caused by: com.alibaba.fastjson.JSONException: autoType is not support. com.cuiyun.kfcoding.framework.cache.sample.model.User
	at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1003) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:316) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:1174) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1342) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.parser.deserializer.JavaObjectDeserializer.deserialze(JavaObjectDeserializer.java:45) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:654) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:365) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:328) ~[fastjson-1.2.44.jar:na]
	at com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer.deserialize(GenericFastJsonRedisSerializer.java:37) ~[fastjson-1.2.44.jar:na]
	... 69 common frames omitted

请问在springboot2.x版本添加ParserConfig.getGlobalInstance().setAutoTypeSupport(true);代码兼容springboot1.x版本么?

cqoc49vn

cqoc49vn1#

并且我看源码 发现在GenericFastJsonRedisSerializer也设置了defaultRedisConfig.setAutoTypeSupport(true)

源码代码如下

public class GenericFastJsonRedisSerializer implements RedisSerializer<Object> {
    private final static ParserConfig defaultRedisConfig = new ParserConfig();
    static { defaultRedisConfig.setAutoTypeSupport(true);}

    @Override
    public byte[] serialize(Object object) throws SerializationException {
        if (object == null) {
            return new byte[0];
        }
        try {
            return JSON.toJSONBytes(object, SerializerFeature.WriteClassName);
        } catch (Exception ex) {
            throw new SerializationException("Could not serialize: " + ex.getMessage(), ex);
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        try {
            return JSON.parseObject(new String(bytes, IOUtils.UTF8), Object.class, defaultRedisConfig);
        } catch (Exception ex) {
            throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
        }
    }
}

版本是fastjson版本是1.2.44

83qze16e

83qze16e2#

fastjson版本1.2.49 有上面问题

xxe27gdn

xxe27gdn3#

spring boot 2.0.3.RELEASE + kotlin
fastjson 1.2.49
也有上面的问题

tp5buhyn

tp5buhyn4#

问下 这个问题解决了吗?

goucqfw6

goucqfw65#

1.2.55版本也遇到这个问题了

看了下源码,是因为实体类存在多参构造方法才抛出的异常,实体类删掉多参构造器就可以了。

dtcbnfnu

dtcbnfnu6#

kotlin 1.2.55 一样的问题

5rgfhyps

5rgfhyps7#

@NightFarmer ,若多参构造器是需要用的,那咋解?

py49o6xq

py49o6xq8#

若存在多参构造器,增加无参构造器可解决

相关问题