Skip to content

[Java] deserialize object,error 'A cannot be cast to class B' occurred #2005

@yyjyc

Description

@yyjyc

Question

I use fury in my projects, in one of the projects i serialize the object of ProductPO.class where fury is created like "furySerializer" in following code, in the other project i deseralize the byte[] using fury like “furyDeSerializer1” in following code.
however,when i deseralize the byte[],an error occurred and made me confused:

java.lang.ClassCastException: class ...RateDetailPO cannot be cast to class ...ProductPO (...RateDetailPO and ...ProductPO are in unnamed module of loader 'app')
	at ...FuryTestV2.test1(FuryTestV2.java:73)
	at ...main(FuryTestV2.java:63)

my code is like:

public class FuryTestV2{
    private static ThreadSafeFury furySerializer = null;
    private static ThreadSafeFury furyDeSerializer1 = null;
    private static ThreadSafeFury furyDeSerializer2 = null;
    static {
        furySerializer = new ThreadLocalFury(classLoader -> {
            Fury f = Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).withLanguage(Language.JAVA).withClassLoader(classLoader)
                    .withDeserializeNonexistentClass(true).build();
            f.register(ProductPO.class);
            return f;
        });

        furyDeSerializer1 = new ThreadLocalFury(classLoader -> {
            Fury f = Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).withLanguage(Language.JAVA).withClassLoader(classLoader)
                    .withDeserializeNonexistentClass(true).build();
            f.register(RateDetailPO.class);
            f.register(PersonRulePO.class);
            f.register(DailyInfoPO.class);
            f.register(DailyInfoList.class);
            f.register(GregorianCalendar.class);
            f.register(com.ctrip.hotel.productsearchfilter.ProductPO.class);
            return f;
        });

        furyDeSerializer2 = new ThreadLocalFury(classLoader -> {
            Fury f = Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).withLanguage(Language.JAVA).withClassLoader(classLoader)
                    .withDeserializeNonexistentClass(true).build();
            f.register(com.ctrip.hotel.productsearchfilter.ProductPO.class);
            f.register(RateDetailPO.class);
            f.register(PersonRulePO.class);
            f.register(DailyInfoPO.class);
            f.register(DailyInfoList.class);
            f.register(GregorianCalendar.class);
            return f;
        });
    }

    public static void main(String[] args) {
        // 构建序列化的对象
        ProductPO productPO = createProductPO();
        // 序列化
        System.out.println("test1:" + test1(productPO)); // false
        System.out.println("test2:" + test2(productPO)); // true

    }

    private static boolean test1(ProductPO productPO) {
        try {
            // 序列化
            byte[] serialize = furySerializer.serialize(productPO);
            // 反序列化
            com.ctrip.hotel.productsearchfilter.ProductPO result = (com.ctrip.hotel.productsearchfilter.ProductPO) furyDeSerializer1.deserialize(serialize);
            System.out.println(SSJsonSerializerUtils.serialize(result));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    private static boolean test2(ProductPO productPO) {
        try {
            // 序列化
            byte[] serialize = furySerializer.serialize(productPO);
            // 反序列化
            com.ctrip.hotel.productsearchfilter.ProductPO result = (com.ctrip.hotel.productsearchfilter.ProductPO) furyDeSerializer2.deserialize(serialize);
            System.out.println(SSJsonSerializerUtils.serialize(result));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static ProductPO createProductPO() {
        ProductPO productPO = new ProductPO();
        productPO.setProductId(1112324L);
        productPO.setHotelId(1234);
        productPO.setMasterHotelId(1235);
        productPO.setBasicRoomId(54435);
        productPO.setMasterBasicRoomId(6234);
        productPO.setRateCodeId(564332);
        productPO.setVendorId(6564);
        productPO.setRoomClass(6563423525L);
        productPO.setPerson(5);
        productPO.setResourceType(3);
        productPO.setPromoteRoomType(true);
        return productPO;
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions