Register a CustomConverter in a MongoTemplate with Spring Boot

Fabio Ebner

How can I register a custom converter in my MongoTemplate with Spring Boot? I would like to do this only using annotations if possible.

Zubair Nabi

You need to create a configuration class for converter config.

@Configuration
@EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class })
@Profile("!testing")
public class MongoConfig extends AbstractMongoConfiguration {
    @Value("${spring.data.mongodb.host}")  //if it is stored in application.yml, else hard code it here
    private String host;

    @Value("${spring.data.mongodb.port}")
    private Integer port;

    @Override
    protected String getDatabaseName() {
        return "test";
    }

    @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient(host, port);
    }

    @Override
    public String getMappingBasePackage() {
        return "com.base.package";
    }

    @Override
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new LongToDateTimeConverter());
        return new CustomConversions(converters);
    }
}
@ReadingConverter
static class LongToDateTimeConverter implements Converter<Long, Date> {
    @Override
    public Date convert(Long source) {
        if (source == null) {
            return null;
        }
        return new Date(source);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MongoDb compact command using spring boot MongoTemplate

Check MongoDB connection using MongoTemplate in Spring Boot

Sorting MongoDB results using Spring Boot with MongoTemplate

How to register ServletContextListener in spring boot

Spring boot register an instance as a bean

Register Bean Validation ValueExtractor with Spring/Spring Boot

Register Spring Converter Programmatically in Spring Boot

Spring Boot Client Failed to register with Admin

Spring-boot: register mongodb custom converter

How to register new collection factories in Spring Boot

Spring Boot App fails to register RequestContextListener

Register Geolatte Jackson module in Spring Boot 3.2

register custom log appender in spring boot starter

Docker and Eureka with Spring Boot failing to register clients

SymmetricDS can not register to server with Spring boot

How to register a custom HttpMessageConvertor in spring-boot?

how to register anonymous HealthIndicators in Spring Boot Actuator

Automatically register XA Resource Spring Boot

Is Spring mongoTemplate thread safe?

Mongodb query to Spring mongoTemplate

How to configure MongoTemplate on Spring?

Issue with update 2nd level nested array element using spring boot mongotemplate

How to register spring boot microservices on spring cloud Netflix eureka?

Spring MongoTemplate - find by regex in collection

Need to replicate query in spring mongotemplate

Convert mongo request with MongoTemplate (spring)

How to Register beans from all sub packages in Spring boot app?

How can I register a secondary servlet with Spring Boot?

How to register python microservices with my eureka server (spring boot)