Tag: MongoDB

Springboot 2 @DataMongoTest错误

我试图在使用kotlin的springboot应用程序中运行一些测试。 挑战是@DataMongoTest中的嵌入式mongodb在随机端口上打开,而弹簧引导数据mongo正在默认端口上寻找连接 我的配置如下: @Configuration @ComponentScan(basePackages = [“zw.co.ift.creditrating.persistence”]) @EnableReactiveMongoRepositories(basePackages = [(“zw.co.ift.creditrating.persistence.repository”)]) class PersistenceConfiguration : AbstractReactiveMongoConfiguration() { @Bean override fun reactiveMongoClient() = MongoClients.create() override fun getDatabaseName() = “bank” } 我的测试如下: @DataMongoTest @ContextConfiguration(classes = [(PersistenceIntegrationConfiguration::class)]) @EnableAutoConfiguration @ExtendWith(SpringExtension::class) 现在以下是我正在运行的测试的输出: 2017-12-07 20:15:56.910 INFO 52176 — [Thread-7] osbamongo.embedded.EmbeddedMongo:2017-12-07T20:15:56.909 + 0200 I CONTROL [initandlisten] MongoDB starting:pid = 52177 port = 61313 dbpath […]

Spring WebFlux:Reactive MongoDB

我是Spring Reactor的新手,所以我想重构这个简单的spring数据(在kotlin)方法: fun save(user: User): Mono { if (findByEmail(user.email).block() != null) { throw UserAlreadyExistsException() } user.password = passwordEncoder.encode(user.password) return userRepository.save(user) } 谢谢

无法通过Springboot连接到远程MongoDB

我试图连接到一个远程的MongoDB。 由于我没有得到一个MongoOpenSocketException,我认为已经建立了一个连接,但需要更多的启动应用程序。 我错过了什么? Application.kt package hello import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration import org.springframework.context.annotation.Configuration /** * The main entry point to the application */ @EnableAutoConfiguration(exclude = arrayOf(MongoAutoConfiguration::class)) @Configuration class Application /** * Run the application * @param args The command line arguments */ fun main(args: Array) { SpringApplication.run(Application::class.java, *args) } 的build.gradle: // Top-level build file […]

Spring数据mongodb和集合模式设计

朋友,我有一个问题,如何组织和注释我的实体和弹簧数据配置为不同的关系? 我有关系的用户,钱包和余额实体: 一对一:用户钱包 一对多:钱包余额 我想在MongoDB中有下一个模式: > db.users.findOne() { _id : ObjectID(‘UUUU’), name : ‘Jason Statham’ } > db.wallets.findOne() { _id : ObjectID(‘WWWW’), user : ObjectID(‘UUUU’) balances : [ { currency: ‘USD’, balance: 100}, { currency: ‘EUR’, balance: 530.50 } ] } 电子钱包应该包含对ObjectID的引用,并且User被存储在单独的集合中 电子钱包应包含作为嵌入实体的余额 实体: data class User(val id: String, val name: String) data class Wallet(val […]

有什么办法可以实现春季webflux的分页和弹簧数据的反应

我试图了解Spring 5的反应部分。我已经创建了简单的rest端点,用于查找所有使用spring web-flux和spring data reactive(mongo)的实体,但是没有看到如何实现分页。 这是我在Kotlin的简单例子: @GetMapping(“/posts/”) fun getAllPosts() = postRepository.findAll() 这是否意味着反应终点不需要分页? 有什么办法从服务器端使用这个堆栈实现分页?

在Spring Boot M7和MongoDB中找不到ReflectKotlinClass

如果使用Web和MongoDB模块在start.spring.io上创建一个新的Spring Boot项目,请添加任何@Document注释类,并且将kotlin.version从1.2.10升级到1.2.21,您将得到一个NoClassDefFoundError: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.M7:run (default-cli) on project demo: An exception occurred while running. null: InvocationTargetException: Error creating bean with name ‘mongoTemplate’ defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘mongoTemplate’ parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘mappingMongoConverter’ defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied […]

在Spring Boot 2中解开Thymeleaf中的Kotlin HashMap

我有一个Kotlin函数,它创建一个带有hashmap的模型,如下所示 @GetMapping(“/”) fun index(model: Model): Mono { model.addAttribute(“images”, imageService.findAllImages()?.flatMap { image -> Mono.just(image) .zipWith(repository.findByImageId(image?.id!!).collectList()) .map({ imageAndComments: Tuple2<Image?, MutableList> -> hashMapOf( “id” to imageAndComments.t1?.id, “name” to imageAndComments.t1?.name, “comments” to imageAndComments.t2) }).log(“findAllImages”) }) model.addAttribute(“extra”, “DevTools can also detech code changes.”) return Mono.just(“index”) } Image.kt package learningspringboot.images import org.springframework.data.annotation.Id data class Image(@Id var id: String? = null, var […]

Kotlin&Vertx&Mongo:如何管理异步CRUDfunction?

朋友们! 我是Vertx和Mongo的绿手,现在我面临一个棘手的问题。 以下是代码段。 这里是Mongo客户端的包装类。 // MongoDatabase.kt import io.vertx.core.json.JsonObject import io.vertx.core.logging.LoggerFactory import io.vertx.kotlin.core.json.JsonObject import io.vertx.rxjava.core.Vertx import io.vertx.rxjava.ext.mongo.MongoClient import kotlin.reflect.KClass import kotlin.reflect.full.declaredFunctions class MongoDatabase (val tClass: KClass, vertx: Vertx, config: JsonObject, databaseName: String) : Database { private val mongo = MongoClient.createShared(vertx, config, databaseName) private val logger = LoggerFactory.getLogger(MongoDatabase::class.java) // I use a self-made annotation to record the […]

ReactiveCrudRespository永远不会从SaveAll返回

在启动时,我检查一些数据,如果不存在尝试保存一些默认值(暂时用于测试)。 val subs = repo.findAll().toIterable() if(subs.none()) { repo.saveAll(defaults.map { Source(it.link.hashCode().toLong(), it::class.java.canonicalName, arrayOf(it.link)) }).blockLast() } 在第一次运行时,我们将达到saveAll(),但从不解锁。 数据保存在MongoDB中,我可以用Robo 3t来确认。 随后运行的数据实际存在将导致第一次findAll从不解锁。 在MongoDB中的分析似乎显示出一个成功的查询。 findAll()查询的配置文件 我的资源库和实体如下: interface SourceRepository : ReactiveCrudRepository<Source, Long> { // } data class Source( @Id val id: Long, val type: String, val params: Array<String> ) 这是在Kotlin,反对Spring Boot 2.0.0.M4。 我的目标是在Docker中运行的MongoDB实例。 如果我删除了这一点的启动逻辑,我的另一个ReactiveCrudRepository能够读/写就好,永远不会阻塞。 工作仓库的saveAll()调用也在blockLast()中结束,因为我发现没有这个保存就不会实际发生。

不能使用MongoDB的spring验证

我在Kotlin寻找了在春季验证数据模型的解决方案。 但我不能。 以下是标签data class 。 data class Tag(@field:NotNull var name: String) { lateinit @Id var id: ObjectId } 我已经启用配置来使用bean验证 @Configuration open class ValidatorConfig { @Bean open fun validator() = LocalValidatorFactoryBean() } 据我所知,Spring Boot会自动加载所有的Bean配置。 在运行时,当用空的Json POST时,应该抛出适当的验证错误,但会抛出以下错误 Instantiation of [simple type, class kagaz.sano.model.Tag] value failed for JSON property name due to missing (therefore NULL) value for creator […]