Spring Boot集成Infinispan的序列化配置问题求助
Infinispan 14 序列化配置问题解决方案
一、解决ISPN000936反序列化拦截错误
即使配置了反序列化白名单仍报错,通常是配置范围或方式不对,按以下步骤排查:
- 确认白名单覆盖范围:全局配置需包含所有涉及序列化的类(含父类、内部类),在
application.yml中配置:infinispan: deserialization: whitelist: classes: com.yourpackage.** accept-list-enabled: true - 缓存级单独配置:如果部分缓存有特殊需求,可在缓存配置中指定白名单:
@Bean public EmbeddedCacheManager cacheManager() { Configuration cacheConfig = new ConfigurationBuilder() .serialization() .whiteList() .addRegex("com.yourpackage.cache.*") .build(); return new DefaultCacheManager(cacheConfig); } - 检查拼写错误:确保类的全限定名或通配符无拼写错误,严格区分大小写。
二、配置JSON序列化方案
Infinispan支持Jackson作为JSON序列化器,步骤如下:
- 引入JSON序列化依赖:
<dependency> <groupId>org.infinispan</groupId> <artifactId>infinispan-json</artifactId> <version>14.0.0.Final</version> </dependency> - 配置缓存使用JSON编码:
在application.yml中全局配置:
或Java代码配置:infinispan: cache: default: encoding: key: media-type: application/json value: media-type: application/json marshaller: org.infinispan.commons.marshall.Jackson2Marshaller@Configuration public class InfinispanJsonConfig { @Bean public CacheManager cacheManager() { ConfigurationBuilder config = new ConfigurationBuilder(); config.encoding() .key().mediaType("application/json") .value().mediaType("application/json") .marshaller(new Jackson2Marshaller()); return new DefaultCacheManager(config.build()); } } - 处理多态POJO:若POJO存在继承关系,需在父类添加Jackson多态注解:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type") @JsonSubTypes({ @JsonSubTypes.Type(value = ChildPojo.class, name = "child") }) public abstract class ParentPojo { // 父类字段 }
三、免手动添加Protobuf注解的方案
方案1:ProtoStream自动注解处理器
通过注解处理器自动生成Protobuf Schema和Marshaller,无需手动编写注解:
- 引入注解处理器依赖:
<dependency> <groupId>org.infinispan.protostream</groupId> <artifactId>protostream-processor</artifactId> <version>4.5.0.Final</version> <scope>provided</scope> </dependency> - 定义Schema生成接口:
@AutoProtoSchemaBuilder( includeClasses = {ParentPojo.class, ChildPojo.class}, schemaPackageName = "com.yourpackage.proto" ) public interface AppProtoSchema extends GeneratedSchema { } - 注册自动生成的Schema:
@Bean public EmbeddedCacheManager cacheManager() { ProtoStreamMarshaller marshaller = new ProtoStreamMarshaller(); marshaller.registerSchema(new AppProtoSchemaImpl()); // 编译时自动生成的实现类 return new DefaultCacheManager( new ConfigurationBuilder() .encoding().marshaller(marshaller) .build() ); }
方案2:反射式Protobuf Marshaller
完全无需注解,基于反射动态生成Schema,适合快速测试(性能略低于自动生成方案):
@Bean public EmbeddedCacheManager cacheManager() { ProtoStreamMarshaller marshaller = new ProtoStreamMarshaller(); // 动态生成Schema String schema = new ReflectiveProtoSchemaBuilder() .fileName("app_schema.proto") .packageName("com.yourpackage.proto") .addClass(ParentPojo.class) .addClass(ChildPojo.class) .build(); marshaller.registerSchemaFromString(schema); // 注册反射式Marshaller marshaller.registerMarshaller(ParentPojo.class, new ReflectiveMarshaller<>(ParentPojo.class)); marshaller.registerMarshaller(ChildPojo.class, new ReflectiveMarshaller<>(ChildPojo.class)); Configuration config = new ConfigurationBuilder() .encoding().marshaller(marshaller) .build(); return new DefaultCacheManager(config); }
内容的提问来源于stack exchange,提问作者usr-local-ΕΨΗΕΛΩΝ
相关产品推荐
相关产品推荐

