Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
Invalid format for createdBy in SpringBoot Auditable

I implemented SpringBoot Auditable and everything is working fine except the createdBy is giving timestamp.

Here is my Auditable class:

@Getter
@Setter
public class Auditable {

    @CreatedDate
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime updatedAt;

    @CreatedBy
    private ObjectId createdBy;

    @LastModifiedBy
    private ObjectId updatedBy;

}

And here is my AuditorAwareImpl class:

public class AuditorAwareImpl implements AuditorAware<ObjectId> {

    @Override
    public Optional<ObjectId> getCurrentAuditor() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        ObjectId userId = null;
        if (authentication != null) {
            UserDetails userDetails = (UserDetails) authentication.getPrincipal();

            userId = new ObjectId(userDetails.getId());
        }
        return Optional.of(userId);
    }
}

It's(ObjectId) saving fine to DB. But when I retrieve that record, I'm getting createdBy as different format instead of ObjectId:

enter image description here

Can someone help me with this please?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Just add this bean convert the ObjectId to String.

Dependency

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.0.rc1</version>
</dependency>

And Add the bean

@SpringBootApplication
public class DivisionServiceApplication {

  public static void main(String[] args) {
    SpringApplication.run(DivisionServiceApplication.class, args);
  }
  
  @Bean
  public Jackson2ObjectMapperBuilderCustomizer customizer() {
    return builder -> builder.serializerByType(ObjectId.class, new ToStringSerializer());
  }  
  
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda