Organizational Research By

Surprising Reserch Topic

Experts Most Trusted 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

Walkin Jobs Opening

Government Jobs Opening