【发布时间】:2021-09-22 00:12:33
【问题描述】:
考虑产生以下输出:
案例1:同时使用spring data neo4j接口(投影)从neo4j中检索嵌套对象
{
"name": "Dhoni",
"currentLocation": {
"city": {
"name": "qwerqwer",
"regionalState": {
"state": {
"name": "zxvcxzcvc"
}
}
}
}
}
投影界面:
public interface PersonProjection {//Root
String getName();
CurrentLocationProjection getCurrentLocation();
public interface CurrentLocationProjection { // has relationship Props
CityProjection getCity(); // target object in entity mapping
interface CityProjection {
String getName();
RegionalStateProjection getRegionalState();
interface RegionalStateProjection { // has relationship Props
StateProjection getState(); // target object in entity mapping
interface StateProjection {
String getName();
}
}
}
}
}
案例 2:当我尝试使用 SpEL 将嵌套值映射到第一级时,工作正常,如下所示
public interface PersonProjection {//Root
String getName();
CurrentLocationProjection getCurrentLocation();
public interface CurrentLocationProjection {
@Value("#{target.city.name}")
String getCityName();
@Value("#{target.city.regionalState.state.name}")
String getStateName();
}
}
输出如下:
{
"name": "Deva",
"currentLocation": {
"cityName": "qwerqwer",
"stateName": "zxvcxzcvc"
}
}
案例 3: 问题: 但是,当我尝试通过 @Value 使用 SpEL 将嵌套值映射到(根级别/根节点)本身时,如下所示:
public interface PersonProjection {
String getName();
@Value("#{target.currentLocation.city.name}")
String getCityName();
@Value("#{target.currentLocation.city.regionalState.name}")
String getStateName();
}
预期输出:
{
"name": "Dhoni",
"cityName": "qwerqwer",
"stateName": "zxvcxzcvc"
}
但是,它会抛出以下错误,说明 currentLocation 为 null,如下所示:(但它有值)
WARN 13600 --- [nio-8080-exec-6] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotWritableException:无法写入 JSON:EL1007E:无法找到属性或字段“城市”空值;嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: EL1007E: 在 null 上找不到属性或字段“城市”(通过引用链:com.sun.proxy.$Proxy162["currentLocation"])]
我有点困惑它是如何在第一级而不是在根级工作的? 有人可以指出我的错误/指导我实现预期的输出。谢谢。
注意:SDN (Spring Data Neo4j) 版本 6.1.2 供参考。
【问题讨论】:
标签: spring-data-neo4j spring-expression-language spring-projections