春天 - >使用Spring Converter将cassandra列数据值从long转换为Date

时间:2015-03-09 08:33:51

标签: spring cassandra spring-data spring-data-cassandra converters

我的问题如下

  1. 我在cassandra表中有一个BigInt列
  2. 在相应的POJO中,我有一个日期字段
  3. 当我使用Spring Cassandra Repository将数据插入cassandra表时,Date会自动转换为Long
  4. 当我尝试从表中选择行时,我在Spring尝试将Long的值映射到日期时出现错误
  5. 虽然我已经实现了自定义转换器

    public class LongToDateConverter implements Converter<Long,Date> {
    
            public Date convert(final Long source) {
    
                if(null!= source)
                {
                    return new Date(source);
                }
                else
                {
                    return null;
                }
            }    
    }
    

    并在spring config

    中注册
    <mvc:annotation-driven />
        <context:component-scan base-package="com" />
        <context:annotation-config />    
        <bean id="conversionService"                    class="org.springframework.context.support.ConversionServiceFactoryBean">
         <property name="converters">
    <list>
    <bean class="com.app.converter.LongToDateConverter" />
    </list>
    </property>
    

    我也有一个cassandra配置

    <cassandra:cluster contact-points="localhost" port="9042" />
    <cassandra:session keyspace-name="xxxx" />
    <cassandra:mapping />
    <cassandra:converter />
    <cassandra:template id="cassandraTemplate" />
    

       

    但我仍然得到例外

    例外情况如下

    org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.Long to type java.util.Date
        org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:276)
        org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:171)
        org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:153)
        org.springframework.data.mapping.model.BeanWrapper.getPotentiallyConvertedValue(BeanWrapper.java:157)
        org.springframework.data.mapping.model.BeanWrapper.setProperty(BeanWrapper.java:75)
        org.springframework.data.cassandra.convert.MappingCassandraConverter.readPropertyFromRow(MappingCassandraConverter.java:187)
        org.springframework.data.cassandra.convert.MappingCassandraConverter$1.doWithPersistentProperty(MappingCassandraConverter.java:150)
        org.springframework.data.cassandra.convert.MappingCassandraConverter$1.doWithPersistentProperty(MappingCassandraConverter.java:145)
        org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:307)
        org.springframework.data.cassandra.convert.MappingCassandraConverter.readPropertiesFromRow(MappingCassandraConverter.java:145)
        org.springframework.data.cassandra.convert.MappingCassandraConverter.readEntityFromRow(MappingCassandraConverter.java:137)
        org.springframework.data.cassandra.convert.MappingCassandraConverter.readRow(MappingCassandraConverter.java:114)
        org.springframework.data.cassandra.convert.MappingCassandraConverter.read(MappingCassandraConverter.java:202)
        org.springframework.data.cassandra.core.CassandraConverterRowCallback.doWith(CassandraConverterRowCallback.java:47)
        org.springframework.data.cassandra.core.CassandraTemplate.selectOne(CassandraTemplate.java:491)
        org.springframework.data.cassandra.core.CassandraTemplate.selectOne(CassandraTemplate.java:365)
        org.springframework.data.cassandra.core.CassandraTemplate.selectOneById(CassandraTemplate.java:286)
        org.springframework.data.cassandra.repository.support.SimpleCassandraRepository.findOne(SimpleCassandraRepository.java:69)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        java.lang.reflect.Method.invoke(Unknown Source)
        org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:405)
        org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:390)
        org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:344)
        org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        $Proxy17.findOne(Unknown Source)
        com.app.services.UserProfileService.get(UserProfileService.java:21)
        com.social.facebook.PostFaceBookSocialLogin.initiate(PostFaceBookSocialLogin.java:87)
        com.social.facebook.FacebookController.facebookAccess(FacebookController.java:87)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        java.lang.reflect.Method.invoke(Unknown Source)
        org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
        org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
        org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
        org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
        org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
        org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
        org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:98)
        org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
        org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:102)
        org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
        org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:102)
        org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:82)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
        org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
        org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
        org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
        org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
        org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
        org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    

0 个答案:

没有答案