2016-07-22 126 views
1

爲了嘗試爲我現有的應用程序外化tomcat會話,我正在嘗試Spring會話Redis解決方案。下面的步驟後,包括在pom.xml中必要的依賴,像這樣:春季會話Redis序列化器SerializationException

<dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${org.springframework-version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.session</groupId> 
      <artifactId>spring-session-data-redis</artifactId> 
      <version>1.2.1.RELEASE</version> 
     </dependency> 

加入springSessionRepositoryFilter在web.xml是這樣的:

<filter> 
     <filter-name>springSessionRepositoryFilter</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSessionRepositoryFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

,並添加在Spring XML配置以下

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/> 

<context:property-placeholder location="classpath:application.properties"/> 

<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:port="${spring.redis.port}"/> 

並且構建和部署到tomcat上,t他是我得到的錯誤:

org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: com.sun.jersey.client.apache.ApacheHttpClient 

任何建議或幫助是極大的讚賞。謝謝 !! 還附有是我的pom.xml的條目: pom.xml entries

+0

你能發佈異常的完整堆棧跟蹤嗎? –

+0

'java.io.NotSerializableException'我猜你試圖保存的任何對象,因爲會話狀態是不可序列化的。發佈更多關於保存會話狀態 –

回答

1

從你的例外,com.sun.jersey.client.apache.ApacheHttpClient不可序列化,因爲它沒有實現java.io.Serializable接口。

需要序列ApacheHttpClient在一些其他的方式,因爲它是一個第三方庫。

您可以使用Jackson庫中的org.codehaus.jackson.map.ObjectMapper來實現此目的。請致電example

你也可以嘗試使用HttpClient附帶的SerializableEntity類。

httpost.setEntity(new SerializableEntity(mySerializableObj, false)); 

下面是一般的方法,使一類的序列化。

  • 如果類是你的,使它序列化(這是你的情況)
  • 如果類是第三方,但你並不需要它的序列化形式,紀念字段作爲短暫
  • ,如果你需要它的數據和它的第三方,考慮系列化 其他手段,如JSON,XML,BSON,MessagePack等 那裏你可以得到第三方的對象序列化的智慧hout修改 他們的定義。
+0

的機制後,嘗試你的建議,現在我得到以下錯誤/異常...我想知道如果我遇到與Spring會話兼容性問題...'代碼'org.springframework .data.redis.serializer.SerializationException:無法序列化;嵌套異常是org.springframework.core.serializer.support.SerializationFailedException:使用DefaultSerializer序列化對象失敗;嵌套異常是java.io.NotSerializableException:org.springframework.context.support.ReloadableResourceBundleMessageSource – joeflux

0

只是想說明這裏可能的解決方案之一。爲簡潔起見,我假設你正在使用彈簧引導。

實質上,當您使用@EnableRedisHttpSession註釋您的應用程序時,spring引導會自動配置RedisHttpSessionConfiguration類中定義的bean。在自動配置的beans中,sessionRedisTemplate就是您想要自定義序列化程序的內容。下面的代碼顯示了RedisHttpSessionConfiguration提供的默認bean定義。

@Bean 
public RedisTemplate<Object, Object> sessionRedisTemplate(
     RedisConnectionFactory connectionFactory) { 
    RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); 
    template.setKeySerializer(new StringRedisSerializer()); 
    template.setHashKeySerializer(new StringRedisSerializer()); 
    if (this.defaultRedisSerializer != null) { 
     template.setDefaultSerializer(this.defaultRedisSerializer); 
    } 
    template.setConnectionFactory(connectionFactory); 
    return template; 
} 

要覆蓋這個bean的配置,簡單的聲明名爲sessionRedisTemplate在@Configuration類之一(例如,HttpSessionConfig)豆,和Spring開機都會做魔法覆蓋它的默認設置。在這裏,我演示了GenericJackson2JsonSerializer

@Configuration 
public class HttpSessionConfig { 

@Bean 
public RedisTemplate<Object, Object> sessionRedisTemplate(
     RedisConnectionFactory connectionFactory) { 
    RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); 
    template.setKeySerializer(new StringRedisSerializer()); 
    template.setHashKeySerializer(new StringRedisSerializer()); 

    template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer()); 

    template.setConnectionFactory(connectionFactory); 
    return template; 
} 
}