1

this is sample project of spring-data-redis + caching. I'd like to store caches in redis in plain json, so I've configured GenericJackson2JsonRedisSerializer with objectMapper provided by spring.

Currently when I run test (the only one in application) I'm getting following error:

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.github.bilakpoc.rediscachedemo.generated.model.ModelImport (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.github.bilakpoc.rediscachedemo.generated.model.ModelImport is in unnamed module of loader 'app')
    at com.github.bilakpoc.rediscachedemo.service.ImportService$$EnhancerBySpringCGLIB$$14512ec6.getImportById(<generated>) ~[classes/:na]

Please can anybody share how to properly configure redis to store caches in json?

Thanks

1 Answer 1

1

Thanks for the reproducer. You do not need to configure redis cache from code. Only problem was "generated code was not serializable" send you the fix in https://github.com/bilak-poc/redis-cache-demo/pull/1

For future reference

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>${openapi-generator-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>openapi</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <apiPackage>${openapi-generator.package}.api</apiPackage>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <serializableModel>true</serializableModel>
                        </configOptions>
                        <generatorName>spring</generatorName>
                        <invokerPackage>${openapi-generator.package}.handler</invokerPackage>
                        <inputSpec>${pom.basedir}/src/main/resources/openapi/openapi.yaml</inputSpec>
                        <modelPackage>${openapi-generator.package}.model</modelPackage>
                        <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                    </configuration>
                </execution>
            </executions>
        </plugin>

serializableModel should be added to configOptions. And @EnableCaching will be enough.

I left comments in CacheConfig. Actually found another workaround with Jackson2JsonRedisSerializer then I dig in more and found out the real problem.

Sign up to request clarification or add additional context in comments.

2 Comments

with your config it's only java serializer and not json. so still not able to save caches in redis as json objects.
github.com/ozkanpakdil/redis-cache-demo/blob/master/src/main/… here how it works with json. or looks like it works with json, but I did not check redis side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.