DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Data: Easy MongoDB Migration Using Mongock
  • MongoDB With Spring Boot: A Simple CRUD
  • Advanced Search and Filtering API Using Spring Data and MongoDB

Trending

  • Safeguarding Sensitive Data: Content Detection Technologies in DLP
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Why 99% Accuracy Isn't Good Enough: The Reality of ML Malware Detection
  • Securing Kubernetes in Production With Wiz
  1. DZone
  2. Data Engineering
  3. Databases
  4. Spring Data, MongoDB, and Java 8

Spring Data, MongoDB, and Java 8

Utilizing the latest Spring frameworks to convert Java 8 dates inside of MongoDB, simplifying a once difficult conversion task. Great concise NoSQL and Java tutorial.

By 
Yong Mook Kim user avatar
Yong Mook Kim
·
Aug. 29, 16 · Code Snippet
Likes (15)
Comment
Save
Tweet
Share
32.4K Views

Join the DZone community and get the full member experience.

Join For Free

While saving an object containing the new Java 8 java.time.LocalDateTime, the following error is thrown:

org.springframework.core.convert.ConverterNotFoundException: 
	No converter found capable of converting 
               from type [java.time.LocalDateTime] to type [java.util.Date] org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.LocalDateTime] to type [java.util.Date] 

We are testing:

  1. Spring 4.3.2.RELEASE
  2. Spring Data MongoDB 1.9.2.RELEASE

Is Spring-data supporting the new Java 8 Date APIs (JSR-310)?

1. Spring Data and JSR-310

Yes, Spring-data supports the JSR-310 spec, with a custom convertor – Jsr310Converters, review the following code snippet :

CustomConversions.java

package org.springframework.data.mongodb.core.convert;

public class CustomConversions {

	/**
	 * Creates a new {@link CustomConversions} instance registering the given converters.
	 *
	 * @param converters
	 */
	public CustomConversions(List<?> converters) {

		//...
		List<Object> toRegister = new ArrayList<Object>();

		// Add user provided converters to make sure they can override the defaults
		toRegister.addAll(converters);
		toRegister.add(CustomToStringConverter.INSTANCE);
		toRegister.addAll(MongoConverters.getConvertersToRegister());
		toRegister.addAll(JodaTimeConverters.getConvertersToRegister());
		toRegister.addAll(GeoConverters.getConvertersToRegister());
		toRegister.addAll(Jsr310Converters.getConvertersToRegister());
		toRegister.addAll(ThreeTenBackPortConverters.getConvertersToRegister());

		for (Object c : toRegister) {
			registerConversion(c);
		}

		//...
	}

2. Solution

The above Jsr310Converters will be enabled if you declared a Spring managed bean for MappingMongoConverter and pass as an argument for MongoTemplate. Review the following MongoConfig example:

MongoConfig.java

import org.springframework.data.mongodb.core.convert.MappingMongoConverter;

@Configuration
@ComponentScan(basePackages = {"com.mkyong.db"})
public class MongoConfig {

    //...
	@Autowired
	MongoDbFactory mongoDbFactory;

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, getDefaultMongoConverter());
        return mongoTemplate;

    }

    @Bean
    public MappingMongoConverter getDefaultMongoConverter() throws Exception {

        MappingMongoConverter converter = new MappingMongoConverter(
                new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());

        return converter;
    }

    //...

}

The MappingMongoConverter extends AbstractMongoConverter, and the AbstractMongoConverter is an InitializingBean. If this declared as a Spring managed bean via @Bean, the afterPropertiesSet() will be fired, and register all the default convertors, including the Jsr310Converters

3. FAQs

If the MappingMongoConverter is not managed by Spring (init with a new keyword like below), you must call the .afterPropertiesSet() manually.

MongoConfig.java

@Configuration
@ComponentScan(basePackages = {"com.mkyong.db"})
public class MongoConfig {

    @Autowired
    MongoDbFactory mongoDbFactory;

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {

        MappingMongoConverter converter = new MappingMongoConverter(
				new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());

	    //CALL THIS MANULLY, so that all the default convertors will be registered!
		converter.afterPropertiesSet();

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);

        return mongoTemplate;

    }

	//...

References

  1. Github – Custom converters to spring-data-mongodb
  2. Spring IO – MappingMongoConverter JavaDoc
  3. Spring IO – CustomConversions JavaDoc
Spring Framework Spring Data Data (computing) Java (programming language) MongoDB

Published at DZone with permission of Yong Mook Kim, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Data: Easy MongoDB Migration Using Mongock
  • MongoDB With Spring Boot: A Simple CRUD
  • Advanced Search and Filtering API Using Spring Data and MongoDB

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: