spring boot version 2.2.5 if my Entity associate with other Entity, like extends org.springframework.data.jpa.domain.AbstractAuditable that contain createdBy(or lastModifiedBy) field, and the getter of field is defined as "public Optional<OtherEntity> getCreatedBy()", will occur "java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class java.util.Optional!"

then I check and try:

  • org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration

    mapper has `registerModule(jdk8Module)`
    
  • config to registerModule SimpleModule(.addSerializer(Optional.class)), all invalid

    implements `org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer`
    `RepositoryRestConfigurerAdapter` is deprecated
    extends `org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration`
    
  • at runtime, com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField() will invoke ser.serialize()

    this ser is not assign as jdk8Module or configured-my-custom-serializer, is org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.NestedEntitySerializer
    
  • as stated before, I find out

    org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.AssociationOmittingSerializerModifier.changeProperties() will check .isEntity() then .assignSerializer(this.nestedEntitySerializer)
        so Serializer always not use jdk8Module or configured-my-custom-serializer
        so if the field is String, and getter is Optional<String> will work normally
    
  • use @JsonSerialize(using = MyOptionalJsonSerializer.class) to specify at getter

    will occur "java.lang.IllegalStateException: Cannot override _serializer: had a `my.package.config.MyOptionalJsonSerializer`, trying to set to `org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$NestedEntitySerializer`"
    cause  the logic in "com.fasterxml.jackson.databind.ser.BeanPropertyWriter.assignSerializer()", is not allow change _serializer when _serializer is not null (_serializer allready assign as NestedEntitySerializer)
    

I suggest:

  • adopt a unified, don't use Optional-return-getter on associate-entity in entity
  • make Spring Data REST support
  • tell me the right way to resolve it, please forgive me for being foolish

Comment From: wilkinsona

Thanks for the suggestion. Spring Data REST is managed as a separate project and they use JIRA for issue tracking. Unfortunately, we can't move issues from GitHub to JIRA automatically. Please open a JIRA ticket and comment here with a link to it.

Comment From: jricher

FYI this seems to now be tracked at: https://github.com/spring-projects/spring-data-rest/issues/1849