【问题标题】:spring data neo4j rest projection returns null when using SpEL expression on nested object在嵌套对象上使用 SpEL 表达式时,spring 数据 neo4j 休息投影返回 null
【发布时间】: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


    【解决方案1】:

    Spring Data Neo4j 中存在一个关于使用不属于投影的相关实体的 Spring 表达式语言的错误。 https://github.com/spring-projects/spring-data-neo4j/issues/2325

    tl;dr; 它在 Spring Data Neo4j 6.1.3 中得到解决

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2017-01-01
      • 2019-07-09
      • 2015-02-05
      相关资源
      最近更新 更多