> 在使用 Kotlin 和 Neo4j 的 Spring Boot 应用程序中,spring,spring-boot,kotlin,neo4j,spring-data-neo4j"/>

首页 > 解决方案 > 为 Map 使用 @CompositeProperty 注释> 在使用 Kotlin 和 Neo4j 的 Spring Boot 应用程序中

问题描述

我有一个关于在带有 Kotlin 和 Neo4j 的 Spring Boot 应用程序中将 @CompositeProperty 用于 Map<String, Set< String >> 的问题。

plugins {
    id("org.springframework.boot") version "2.5.0-SNAPSHOT"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.4.30"
    kotlin("plugin.spring") version "1.4.30"
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-neo4j")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.+")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

我想在一个实体中有一个 Map,它包含一个字符串作为键和一组字符串作为值(Map<String,Set<String >>)。(它是类中的最后一个属性)

@Node("Skill")
data class Skill(

  @Id
  @GeneratedValue
  @JsonIgnore
  val id: Long?,

  @Property("uuid")
  val uuid: UUID?,

  @CompositeProperty
  val description: Map<String, Set<String>>?,

路由器:

@SpringBootApplication
internal class SkillApplication {

    @Bean
    fun route(handler: SkillHandler) = router {
        GET("$SKILL_ROUTE/$ID_PATH_PATTERN", handler::findByUuid)
        GET(SKILL_ROUTE, handler::findAll)
        POST(SKILL_ROUTE, handler::create)
    }

    companion object {
        const val SKILL_ROUTE = "/skill"
        const val ID_PATH_VAR = "id"
        const val ID_PATH_PATTERN = "{$ID_PATH_VAR:${ID_PATTERN}}"
    }

}

fun main(args: Array<String>) {
    runApplication<SkillApplication>(*args)
}

处理程序:

@Component
internal class SkillHandler(val service: SkillService) {

  fun findAll(request: ServerRequest): Mono<ServerResponse> {
    return ok().body(service.findAll())
  }

fun create(request: ServerRequest): Mono<ServerResponse> {
    return request.bodyToMono<Skill>()
      .flatMap {
        service.create(it).flatMap { skill ->
          ok().body(skill.toMono())
            .switchIfEmpty(badRequest().build())
        }
      }
  }

服务层(关系“层次结构”只是关系的第一个测试):

internal class SkillService(val repo: SkillRepository) {

  fun findAll() = repo.findAll()

fun create(skillRequest: Skill): Mono<Skill> {
    val skill2 = Skill(
      null,
      UUID.randomUUID(),
      mapOf("en" to setOf("english description")),
      emptySet()
    )
    val hierarchy: Set<Hierarchy> = setOf(Hierarchy(null, UUID.randomUUID(), skill2))
    val saveSkill: Skill = skillRequest.copy(uuid = UUID.randomUUID(), hierarchy = hierarchy)
    return repo.save(saveSkill)

在查看数据库浏览器上存储的信息时,我注意到地图存储在属性下,情况并非如此。

{
  "identity": 12,
  "labels": [
    "Skill"
  ],
  "properties": {
"description.en": [
      "english description 2",
      "english description 1"
    ],
"uuid": "2e7924c1-6e53-4faa-be93-8081107279f5"
  }
}

记录实际上应该是这样的:

{
    "uuid": "eeaacee9-782a-42e9-b5a2-5bf2b837addc",
    "description": {
        "en": [
            "english description 2",
            "english description 1"
        ]

但是,当通过 rest 端点检索数据时,我收到以下错误消息。

"status": 500,
    "error": "Internal Server Error",
    "message": "JSON encoding error: class org.neo4j.driver.internal.value.StringValue cannot be cast to class java.lang.String (org.neo4j.driver.internal.value.StringValue is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: class org.neo4j.driver.internal.value.StringValue cannot be cast to class java.lang.String (org.neo4j.driver.internal.value.StringValue is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap') (through reference chain: java.util.ArrayList[0]->com.test.skill.entity.Skill[\"description\"]->java.util.HashMap[\"en\"]->java.util.LinkedHashSet[0])",

我的猜测是我需要 Neo4jPersistentPropertyToMapConverter 接口。我对这个假设是否正确,具体的实现会如何?

非常感谢。

标签: springspring-bootkotlinneo4jspring-data-neo4j

解决方案


推荐阅读