Organizational Research By

Surprising Reserch Topic

set mongodb converter programatically

set mongodb converter programatically  using -'java,mongodb,spring-data-mongodb'

I'm trying to use a custom converter with spring-data-mongodb. I want to create it programmatically, but I get the following error:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of     converting from type org.joda.time.LocalDate to type java.lang.String

The following is the failing code snippet:

    Mongo mongo = new Mongo();
    MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongo, "database");

    List<Converter> converters = new ArrayList<>();
    converters.add(new LocalDateWriteConverter());
    converters.add(new LocalDateReadConverter());
    CustomConversions customConversions = new CustomConversions(converters);

    MappingContext mappingContext = new SimpleMongoMappingContext();
    MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(mongoDbFactory, mappingContext);

    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, mappingMongoConverter);

    MongoDbEvent mongoEvent = new MongoDbEvent(new LocalDate(2012, 12, 8));

And here are my converter classes:

class LocalDateReadConverter implements Converter<String, LocalDate> {
    public LocalDate convert(String s) {
        // Conversion code omitted.

class LocalDateWriteConverter implements Converter<LocalDate, String> {

    public String convert(LocalDate localDate) {
        // Conversion code omitted.

The class I'm trying to persist looks like this:

import org.joda.time.LocalDate;

public class MongoDbEvent {

    private String id;
    private LocalDate date;

    public MongoDbEvent(LocalDate date) { = date;

    public String getId() {
        return id;

    public LocalDate getDate() {
        return date;

    public String toString() {
        return "MongoDbEvent{" +
                "id='" + id + '\'' +
                ", date=" + date +

Any suggestions would be much appreciated.


asked Sep 21, 2015 by akhilesh
0 votes

Related Hot Questions

3 Answers

0 votes

This answer may be a little late for the OP, but I just ran into the same problem today and found a solution...

To set it up programmatically, you need to call MongoMappingConverter.afterPropertiesSet() before you use it. I realized this from reading the code for MongoTemplate.getDefaultMongoConverter(MongoDbFactory).

Here's an example:

MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory, context);
converter.setCustomConversions(new CustomConversions(
                new TimeZoneReadConverter(),
                new TimeZoneWriteConverter()
MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);
answered Sep 21, 2015 by yashwantpinge
0 votes

Just a heads up. I was struggling with that problem on spring-data-mongodb 1.5.1.RELEASEusing Java Configuration. As some classes have changed, I'm posting my solution.

Add the following definition in your configuration class annotated with @Configuration:

public Mongo mongo() throws Exception {
    MongoPropertiesResolver resolver = mongoResolver();
    return new MongoClient(resolver.getUrl());

public MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(mongo(), "database");

public MongoTemplate mongoTemplate() throws Exception {
    return new MongoTemplate(mongoDbFactory(), mongoConverter());

public CustomConversions customConversions() {
    List> converters = new ArrayList>();
    converters.add(new TimeZoneReadConverter());
    converters.add(new TimeZoneReadConverter());
    return new CustomConversions(converters);

public MappingMongoConverter mongoConverter() throws Exception {
    MongoMappingContext mappingContext = new MongoMappingContext();
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
    return mongoConverter;
answered Sep 21, 2015 by patilkiran.101
0 votes

With the introduction of the java.time package in java 8 I ran into a similar issue using the new LocalDate and LocalDateTime classes in the new package. This is how I solved it:

I wrote a converter for all 4 of these conversion options:

  • DateToLocalDateTimeConverter
  • DateToLocalDateConverter
  • LocalDateTimeToDateConverter
  • LocalDateToDateConverter

Here is an example

public class DateToLocalDateTimeConverter implements Converter {

    public LocalDateTime convert(Date source) { 
        return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault()); 

Then by including this in the xml configuration for the mongodb connection I was able to work in java 8 dates with mongodb (remember to add all the converters):


answered Sep 21, 2015 by vickeykumar66