我有一个关于在带有kotlin和neo4j的spring引导应用程序中对map<string,set>使用@compositeproperty的问题。
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>)(它是类中的最后一个属性)
@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)
在查看数据库资源管理器上存储的信息时,我注意到Map存储在properties下,而事实并非如此。
{
"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接口。我的假设正确吗?一个具体的实现看起来如何?
非常感谢你。
暂无答案!
目前还没有任何答案,快来回答吧!