首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用简单spring的JSON序列化

使用简单spring的JSON序列化
EN

Stack Overflow用户
提问于 2015-09-02 01:45:33
回答 1查看 1.4K关注 0票数 1

我无法使用设置为JSON的默认序列化类型的simple-spring。我得到的错误是:

代码语言:javascript
复制
java.lang.IllegalArgumentException: Cannot use JSON serialization because dedicated cache transcoder is null!
at com.google.code.ssm.CacheImpl.set(CacheImpl.java:290) ~[simple-spring-memcached-3.5.0.jar:na]
at com.google.code.ssm.CacheImpl.set(CacheImpl.java:125) ~[simple-spring-memcached-3.5.0.jar:na]
at com.google.code.ssm.PrefixedCacheImpl.set(PrefixedCacheImpl.java:130) ~[simple-spring-memcached-3.5.0.jar:na]
at com.google.code.ssm.spring.SSMCache.put(SSMCache.java:159) ~[spring-cache-3.5.0.jar:na]
at org.springframework.cache.interceptor.CacheAspectSupport.update(CacheAspectSupport.java:351) [spring-context-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:214) [spring-context-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) [spring-context-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) [spring-aop-3.2.9.RELEASE.jar:3.2.9.RELEASE]
at com.sun.proxy.$Proxy105.findByValue(Unknown Source) [na:na]

我的配置是:

代码语言:javascript
复制
    @Configuration()
    @EnableAspectJAutoProxy
    @EnableCaching
    public class SimpleSpringCacheConfig {

    @Autowired
    private Environment env;

    private static final String DEFAULT_MEMCACHED_HOST = "127.0.0.1:11211";
    private static final Integer DEFAULT_MEMCACHED_TTL_SECONDS = 3600;
    private static final Integer DEFAULT_MEMCACHED_TIMEOUT_MILLIS = 500;

    private static final String PROPERTY_MEMCACHED_HOSTS = "service.caching.memcached.hosts";
    private static final String PROPERTY_DEFAULT_TTL_SECONDS="service.caching.default.ttl.seconds";

    private static final Logger log = LoggerFactory.getLogger(SimpleSpringCacheConfig.class);

    //reference config on https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started#Spring_3.1_Cache_Integration
    @Bean
    public CacheManager cacheManager() throws Exception
    {
            MemcacheClientFactoryImpl cacheClientFactory = new MemcacheClientFactoryImpl();
            AddressProvider addressProvider = new DefaultAddressProvider(env.getProperty(PROPERTY_MEMCACHED_HOSTS, DEFAULT_MEMCACHED_HOST));
            CacheConfiguration cacheConfiguration = new CacheConfiguration();

            cacheConfiguration.setKeyPrefixSeparator("_");
            cacheConfiguration.setUseNameAsKeyPrefix(true);
            cacheConfiguration.setConsistentHashing(true);
            cacheConfiguration.setOperationTimeout(DEFAULT_MEMCACHED_TIMEOUT_MILLIS);

            CacheFactory cacheFactory = new CacheFactory();
            cacheFactory.setCacheName("simpleMemcachedCache");
            cacheFactory.setCacheClientFactory(cacheClientFactory);
            cacheFactory.setAddressProvider(addressProvider);
            cacheFactory.setConfiguration(cacheConfiguration);
            cacheFactory.setDefaultSerializationType(SerializationType.JSON);

            Cache object = cacheFactory.getObject();

            int ttl = env.getProperty(PROPERTY_DEFAULT_TTL_SECONDS, Integer.class, DEFAULT_MEMCACHED_TTL_SECONDS);

            //@CacheEvict(..., "allEntries" = true) won't work because allowClear is false, 
            //so we won't flush accidentally all entries from memcached instance..
            SSMCache ssmCache = new SSMCache(object, ttl, false);

            ArrayList<SSMCache> ssmCaches = new ArrayList<SSMCache>();
            ssmCaches.add(0, ssmCache);

            SSMCacheManager ssmCacheManager = new SSMCacheManager();
            ssmCacheManager.setCaches(ssmCaches);

            return ssmCacheManager;
    }
}

根据这里的指南,Started#Serialization似乎没有必要定义自定义转码器。我做错什么了?

我使用以下版本..。spring-cache: v3.5.0 spymemcached-provider: v3.5.0 spring: v3.2.8

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-02 04:58:31

如果使用SSM配置,则不需要定义JSON转换程序。在您的示例中,需要完全初始化CacheFactory对象,因此调用:

代码语言:javascript
复制
cacheFactory.afterPropertiesSet();

就在之前

代码语言:javascript
复制
Cache object = cacheFactory.getObject();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32343427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档