【问题标题】:Spring Cloud config throws NPE empty for array in the fileSpring Cloud config 为文件中的数组抛出 NPE 为空
【发布时间】:2020-08-07 19:26:33
【问题描述】:

我们使用了 Spring Cloud Config 2.1 版,它工作正常。 我们更新到 Spring Cloud Config 2.2,现在它不起作用。 更多细节是 https://github.com/spring-cloud/spring-cloud-config/issues/1599 我也报告了这个问题以加快进程,或者这可能不是问题。我不知道,所以我请你帮忙。

我们的配置文件:python-service.yml

resources:
  - resource1
  - resource2

newResources: []

据我所知,Spring Cloud 配置客户端发出请求以获取配置,并传递标头

Accept: application/vnd.spring-cloud.config-server.v2+json。 在 Spring Cloud 配置 v 2.1 中

注意,Spring Cloud 2.1 版不发送此类标头;相反,它发送Accept: application/json

HTTP http://localhost:8888/python-service/dev
Accept: application/vnd.spring-cloud.config-server.v2+json

返回

{
    "name": "python-service",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "file:/configuration/python-service.yml",
            "source": {
                "resources[0]": "resource1",
                "resources[1]": "resource2",
                "newResources": []
            }
        }
    ]
}

但是,它是 Spring Cloud Config v 2.2,它失败了

{
    "timestamp": "2020-04-24T08:38:19.803+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Could not construct context for config=python-service profile=dev label=null includeOrigin=true; nested exception is java.lang.NullPointerException",
    "path": "/python-service/dev"
}

好笑的是config-service日志中没有异常日志输出!

如果我删除接受标头,我将得到 (version 2.2)

{
    "name": "python-service",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "file:/configuration/python-service.yml",
            "source": {
                "resources[0]": "resource1",
                "resources[1]": "resource2",
                "newResources": ""
            }
        }
    ]
}

这里,为什么"newResources": "" 变成一个空字符串,如果它应该是一个空数组 - 另一个问题。

总结一下

  • 1) Spring Cloud config中如何使用空数组。
  • 2) 为什么 Spring config-service 日志中没有关于 NPE 的日志消息。
  • 3) 如果没有接受头,为什么"newResources": "" 会变成一个空字符串,如果我期望一个空数组。

目前,我可以从我的配置中删除空数组,但这非常可怕,因为我们的配置用于许多服务!这破坏了向后兼容性。

【问题讨论】:

    标签: java spring-boot spring-cloud spring-cloud-config


    【解决方案1】:

    事实证明,这是 Spring Boot 中的一个错误。

    https://github.com/spring-cloud/spring-cloud-config/issues/1572#issuecomment-620496235

    https://github.com/spring-projects/spring-boot/issues/20506

    可能的选择:

    • 1) 等到它修复并更新库。

    • 2) 我们做了什么。我们用空元素替换了空数组。

    newResources:
    anotherField: value
    

    或者,使用null。但是,请确保您的代码可以处理它。此外,emptyArray 可以被视为emptyString。我在调试器中发现了这一点。

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2022-12-04
      • 2018-12-15
      • 2021-03-06
      • 2019-10-13
      • 2016-09-12
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      相关资源
      最近更新 更多