Spring Session Redis serializer SerializationException

joeflux picture joeflux · Jul 22, 2016 · Viewed 8.8k times · Source

In an attempt to externalize tomcat session for my existing application I am trying the Spring Session Redis solution. After following the steps to include necessary dependencies in pom.xml like so :

<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>

adding the springSessionRepositoryFilter in web.xml like this :

<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>

and adding the following in Spring XML configuration

<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}"/>

and building and deploying on to tomcat, this is the error I am getting :

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

Any suggestion to or help is greatly appreciated. Thanks !! Also attached are my pom.xml entries : pom.xml entries

Answer

Sundararaj Govindasamy picture Sundararaj Govindasamy · Jul 22, 2016

From your exception, com.sun.jersey.client.apache.ApacheHttpClient is not serializable because it did not implemented java.io.Serializable.

You need to serialize ApacheHttpClient in some other way, because it is a 3rd party library.

You can use org.codehaus.jackson.map.ObjectMapper from the Jackson library to achieve this.

Please refer this example.

You could also try SerializableEntity class shipped with HttpClient.

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

Below are the general approaches to make a class serializable.

  • if the class is yours, make it Serializable ( this is not your case)
  • if the class is 3rd party, but you don't need it in the serialized form, mark the field as transient
  • if you need its data and it's third party, consider other means of serialization, like JSON, XML,BSON, MessagePack etc. where you can get 3rd party objects serialized without modifying their definitions.