From 0fe15633887f0e47baf0c8118269e39a7448c8f7 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sun, 24 Jan 2021 23:40:00 +0530 Subject: [PATCH 1/2] JAVA-4012: Moved 1 article from spring-data-mongodb to spring-boot-persistence-mongodb --- .../spring-boot-persistence-mongodb/README.md | 2 + .../zoneddatetime/config/MongoConfig.java | 53 +++++++++++++++++++ .../converter/ZonedDateTimeReadConverter.java | 2 +- .../ZonedDateTimeWriteConverter.java | 2 +- .../baeldung/zoneddatetime}/model/Action.java | 2 +- .../repository/ActionRepository.java | 5 +- .../ActionRepositoryLiveTest.java | 8 +-- .../spring-data-mongodb/README.md | 5 +- .../java/com/baeldung/config/MongoConfig.java | 5 -- 9 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/config/MongoConfig.java rename persistence-modules/{spring-data-mongodb/src/main/java => spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime}/converter/ZonedDateTimeReadConverter.java (88%) rename persistence-modules/{spring-data-mongodb/src/main/java => spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime}/converter/ZonedDateTimeWriteConverter.java (87%) rename persistence-modules/{spring-data-mongodb/src/main/java/com/baeldung => spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime}/model/Action.java (96%) rename persistence-modules/{spring-data-mongodb/src/main/java/com/baeldung => spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime}/repository/ActionRepository.java (60%) rename persistence-modules/{spring-data-mongodb/src/test/java/com/baeldung/repository => spring-boot-persistence-mongodb/src/test/java/com/baeldung/zoneddatetime}/ActionRepositoryLiveTest.java (87%) diff --git a/persistence-modules/spring-boot-persistence-mongodb/README.md b/persistence-modules/spring-boot-persistence-mongodb/README.md index aade8eb1c0..97241ad464 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/README.md +++ b/persistence-modules/spring-boot-persistence-mongodb/README.md @@ -4,3 +4,5 @@ - [Spring Boot Integration Testing with Embedded MongoDB](http://www.baeldung.com/spring-boot-embedded-mongodb) - [Upload and Retrieve Files Using MongoDB and Spring Boot](https://www.baeldung.com/spring-boot-mongodb-upload-file) - [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs) +- [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime) + diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/config/MongoConfig.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/config/MongoConfig.java new file mode 100644 index 0000000000..3cfefa099c --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/config/MongoConfig.java @@ -0,0 +1,53 @@ +package com.baeldung.zoneddatetime.config; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.springframework.context.annotation.Configuration; +import org.springframework.core.convert.converter.Converter; +import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; +import org.springframework.data.mongodb.core.convert.MongoCustomConversions; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.baeldung.zoneddatetime.converter.ZonedDateTimeReadConverter; +import com.baeldung.zoneddatetime.converter.ZonedDateTimeWriteConverter; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; + +@Configuration +@EnableMongoRepositories(basePackages = { "com.baeldung" }) +public class MongoConfig extends AbstractMongoClientConfiguration { + + private final List> converters = new ArrayList>(); + + @Override + protected String getDatabaseName() { + return "test"; + } + + @Override + public MongoClient mongoClient() { + final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test"); + final MongoClientSettings mongoClientSettings = MongoClientSettings.builder() + .applyConnectionString(connectionString) + .build(); + return MongoClients.create(mongoClientSettings); + } + + @Override + public Collection getMappingBasePackages() { + return Collections.singleton("com.baeldung"); + } + + @Override + public MongoCustomConversions customConversions() { + converters.add(new ZonedDateTimeReadConverter()); + converters.add(new ZonedDateTimeWriteConverter()); + return new MongoCustomConversions(converters); + } + +} diff --git a/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeReadConverter.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/converter/ZonedDateTimeReadConverter.java similarity index 88% rename from persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeReadConverter.java rename to persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/converter/ZonedDateTimeReadConverter.java index a2d847957b..f4a4c2d040 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeReadConverter.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/converter/ZonedDateTimeReadConverter.java @@ -1,4 +1,4 @@ -package converter; +package com.baeldung.zoneddatetime.converter; import org.springframework.core.convert.converter.Converter; diff --git a/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeWriteConverter.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/converter/ZonedDateTimeWriteConverter.java similarity index 87% rename from persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeWriteConverter.java rename to persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/converter/ZonedDateTimeWriteConverter.java index e13ac2d130..2244a1460a 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/converter/ZonedDateTimeWriteConverter.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/converter/ZonedDateTimeWriteConverter.java @@ -1,4 +1,4 @@ -package converter; +package com.baeldung.zoneddatetime.converter; import org.springframework.core.convert.converter.Converter; diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Action.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/model/Action.java similarity index 96% rename from persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Action.java rename to persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/model/Action.java index aa480dbdf7..06202fe465 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/model/Action.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/model/Action.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.zoneddatetime.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/ActionRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/repository/ActionRepository.java similarity index 60% rename from persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/ActionRepository.java rename to persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/repository/ActionRepository.java index bdca490fe6..e214c4b3c4 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/repository/ActionRepository.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/zoneddatetime/repository/ActionRepository.java @@ -1,6 +1,7 @@ -package com.baeldung.repository; +package com.baeldung.zoneddatetime.repository; -import com.baeldung.model.Action; import org.springframework.data.mongodb.repository.MongoRepository; +import com.baeldung.zoneddatetime.model.Action; + public interface ActionRepository extends MongoRepository { } \ No newline at end of file diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/ActionRepositoryLiveTest.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/zoneddatetime/ActionRepositoryLiveTest.java similarity index 87% rename from persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/ActionRepositoryLiveTest.java rename to persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/zoneddatetime/ActionRepositoryLiveTest.java index 79648f1a20..3a241418ca 100644 --- a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/repository/ActionRepositoryLiveTest.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/zoneddatetime/ActionRepositoryLiveTest.java @@ -1,7 +1,5 @@ -package com.baeldung.repository; +package com.baeldung.zoneddatetime; -import com.baeldung.config.MongoConfig; -import com.baeldung.model.Action; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -12,6 +10,10 @@ import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import com.baeldung.zoneddatetime.config.MongoConfig; +import com.baeldung.zoneddatetime.model.Action; +import com.baeldung.zoneddatetime.repository.ActionRepository; + import java.time.ZoneOffset; import java.time.ZonedDateTime; diff --git a/persistence-modules/spring-data-mongodb/README.md b/persistence-modules/spring-data-mongodb/README.md index 0be8c57036..acc978c68e 100644 --- a/persistence-modules/spring-data-mongodb/README.md +++ b/persistence-modules/spring-data-mongodb/README.md @@ -9,9 +9,8 @@ - [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb) - [Introduction to Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-tutorial) - [Spring Data MongoDB: Projections and Aggregations](http://www.baeldung.com/spring-data-mongodb-projections-aggregations) -- [Spring Data MongoDB Transactions](https://www.baeldung.com/spring-data-mongodb-transactions ) -- [ZonedDateTime with Spring Data MongoDB](https://www.baeldung.com/spring-data-mongodb-zoneddatetime) - +- [Spring Data Annotations](http://www.baeldung.com/spring-data-annotations) +- [Spring Data MongoDB Transactions](https://www.baeldung.com/spring-data-mongodb-transactions) ## Spring Data MongoDB Live Testing diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java index 8036bbbca2..3819558e8b 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java @@ -25,9 +25,6 @@ import com.mongodb.MongoClientSettings; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; -import converter.ZonedDateTimeReadConverter; -import converter.ZonedDateTimeWriteConverter; - @Configuration @EnableMongoRepositories(basePackages = "com.baeldung.repository") public class MongoConfig extends AbstractMongoClientConfiguration { @@ -69,8 +66,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration { @Override public MongoCustomConversions customConversions() { converters.add(new UserWriterConverter()); - converters.add(new ZonedDateTimeReadConverter()); - converters.add(new ZonedDateTimeWriteConverter()); return new MongoCustomConversions(converters); } From 992a33fe7dd6f2c766df2ddee288243a2c2e7a16 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sun, 24 Jan 2021 23:46:39 +0530 Subject: [PATCH 2/2] JAVA-4012: clean-up --- .../main/java/com/baeldung/config/MongoConfig.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java index 3819558e8b..90b1268133 100644 --- a/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java +++ b/persistence-modules/spring-data-mongodb/src/main/java/com/baeldung/config/MongoConfig.java @@ -5,16 +5,13 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mongodb.MongoDatabaseFactory; import org.springframework.data.mongodb.MongoTransactionManager; import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; -import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MongoCustomConversions; -import org.springframework.data.mongodb.gridfs.GridFsTemplate; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.baeldung.converter.UserWriterConverter; @@ -31,9 +28,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration { private final List> converters = new ArrayList>(); - @Autowired - private MappingMongoConverter mongoConverter; - @Override protected String getDatabaseName() { return "test"; @@ -69,11 +63,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration { return new MongoCustomConversions(converters); } - @Bean - public GridFsTemplate gridFsTemplate() throws Exception { - return new GridFsTemplate(mongoDbFactory(), mongoConverter); - } - @Bean MongoTransactionManager transactionManager(MongoDatabaseFactory dbFactory) { return new MongoTransactionManager(dbFactory);