spring - 为 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 接口。我对这个假设是否正确,具体的实现会如何?
非常感谢。
解决方案
推荐阅读
- python - 无法使 Qmenu 背景弯曲
- javascript - 二维地图数组上的简单 Javascript 碰撞检测
- postgresql-11 - postgres-11:获取版本时未找到扩展
- c++ - 正确处理异常的测试用例类
- python - 为什么对 css 文件的修改在服务器渲染中不起作用?
- mclust - 在 R 中使用 mclust 函数运行聚类分析时出错
- typescript - 数据图像(base64)到普通 url 或 blob url
- json - Logstash:无法将事件索引到 Elasticsearch
- apache-spark - 从 PySpark ParamGrid 中提取 MLP 层
- xcode - 命令 PhaseScriptExecution 因 Alamofire 的非零退出代码而失败