Micronaut + Redis – Fails when there is more than one cache

Viewed 202

I've got two controllers:

@Controller
@CacheConfig("foo")
open class FooController {

    data class Foo(val bar: String)

    private val log = logger(FooController::class)

    @Get(uri = "/foo")
    @Cacheable
    open suspend fun foo(): Foo {
        log.info { "Fooooooiing" }
        return Foo(LocalDateTime.now().toString())
    }
}

@Controller
@CacheConfig("bar")
open class BarController {

    data class Bar(val foo: String)

    private val log = logger(BarController::class)

    @Get(uri = "/bar")
    @Cacheable
    open suspend fun foo(): Bar {
        log.info { "Barring" }
        return Bar(LocalDateTime.now().toString())
    }
}

redis on docker:

  redis:
    container_name: redis
    image: redis:latest
    ports:
      - "6379:6379"

dependencies:

    implementation("io.micronaut:micronaut-runtime:3.2.0")
//...
    implementation("io.micronaut.redis:micronaut-redis-lettuce")
    implementation("io.micronaut.cache:micronaut-cache-core")

and configuration

redis:
  uri: redis://localhost:6379
  caches:
    bar:
      key-serializer: io.micronaut.jackson.serialize.JacksonObjectSerializer
      value-serializer: io.micronaut.jackson.serialize.JacksonObjectSerializer
      charset: UTF-8
    foo:
      key-serializer: io.micronaut.jackson.serialize.JacksonObjectSerializer
      value-serializer: io.micronaut.jackson.serialize.JacksonObjectSerializer
      charset: UTF-8

And when I call GET http://localhost:8085/foo then I get:

{
  "foo": "2021-12-09T14:15:56.381390210"
}

But when I then call GET http://localhost:8085/bar then I get:

Internal Server Error: Error encoding object [Err(error=ServiceError(key=UNHANDLED_ERROR, 
message=Unhandled error occurred in the service: Error deserializing object from JSON: 
Instantiation of [simple type, class .BarController$Bar] value failed for JSON property 
bar due to missing (therefore NULL) value for creator parameter bar which is a non-
nullable type\n at [Source: (byte[])\"{\"foo\":\"2021-12-09T14:15:56.381390210\"}\"

It clearly tries to deserialize foo instead of calling bar and serializing it.

If I kill Redis and restart, and call bar first, then foo fails.

Not sure if it's related by if I remove suspend from a controller function signature then it fails with a different error:

Error serializing object to JSON: No serializer found for class io.micronaut.cache.interceptor.ParametersKey and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)"

How to fix this issue?

Greetings, Lutosław

0 Answers
Related