Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of spring boot integrating mongo to query converter exception

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "spring boot integration mongo query converter exception method", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "spring boot integration mongo query converter exception method"!

Preface

People who have used spring boot know that the idea that spring boot convention is superior to configuration provides us with a lot of convenience to integrate relevant technical frameworks in our development, and it is quite simple to integrate mongo, but there are some problems in integrating mongo through the agreed configuration information.

When your field contains the type Timestamp, a type conversion exception is thrown when reading the data, such as

No converter found capable of converting from type [java.util.Date] to type [java.sql.Timestamp]

Because the time type of mongo itself is Date, Date cannot be forcefully converted to Timestamp when doing the result mapping, which is one of the points. Of course, there are many similar data conversion problems that can be solved by this example.

Therefore, we need a custom converter, and there is no configuration converter for MongoProperties in the spring boot agreement, so we can't simply achieve our configuration through application.properties.

Let's solve the problem by configuring our MongoTemplate with custom converter in the way of java bean

Custom converter import org.springframework.core.convert.converter.Converter;import java.sql.Timestimport java.util.Date;public class TimestampConverter implements Converter {public Timestamp convert (Date date) {if (date! = null) {return new Timestamp (date.getTime ());} return null;}} java bean configuration MongoTemplate/** * Created by kl on 2017-3-22. * Content: MongoTemplate configuration of mongodb * / @ Configurationpublic class MongoDBConfig {@ Bean public MongoTemplate getMongoTemplate (MongoDbFactory dbFactory,MappingMongoConverter converter) {MongoTemplate template = new MongoTemplate (dbFactory, converter); return template;} @ Bean public MappingMongoConverter mappingMongoConverter (MongoDbFactory factory, MongoMappingContext context, BeanFactory beanFactory,CustomConversions conversions) {DbRefResolver dbRefResolver = new DefaultDbRefResolver (factory); MappingMongoConverter mappingConverter = new MappingMongoConverter (dbRefResolver, context); mappingConverter.setCustomConversions (beanFactory.getBean (CustomConversions.class)) MappingConverter.setTypeMapper (new DefaultMongoTypeMapper (null)); / remove _ class mappingConverter.setCustomConversions (conversions) added by default mapper; / / add custom converter return mappingConverter;} @ Bean public CustomConversions customConversions () {List list = new ArrayList (); list.add (new TimestampConverter ()); return new CustomConversions (list);}}

Ps: the default mongo type mapping adds our class full path name to the _ class field of our mongdb, as shown in the following figure, mainly to query the mapping of the fruit subtypes.

If we don't need it, we can remove it by constructing DefaultMongoTypeMapper to pass empty.

Thank you for your reading, the above is the content of "spring boot integrates mongo query converter exception method". After the study of this article, I believe you have a deeper understanding of the problem of spring boot integrating mongo query converter exception method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report