java - 将对象转换为 json 时覆盖属性名称

标签 java json groovy jackson

我正在为工作中的某个功能编写一些后端测试,显然陷入了一些在纸上看起来很容易解决但无法弄清楚如何使其工作的问题上>.>。

这是我正在做的事情的一个例子。

这就是我需要 JSON 最终的样子:

{
    "application.properties.service": {
        "properties": {
            "test": "property"
        }
    }
}

这些是我为模拟该请求而编写的 2 个 Groovy 类:

class Configuration {

    private ApplicationPropertiesService applicationPropertiesService

    Configuration() {

    }

    @JsonProperty("application.properties.service")
    ApplicationPropertiesService getApplicationPropertiesService() {
        return applicationPropertiesService
    }

    void setApplicationPropertiesService(ApplicationPropertiesService applicationPropertiesService) {
        this.applicationPropertiesService = applicationPropertiesService
    }
}

--

class ApplicationPropertiesService {

    private Map properties

    ApplicationPropertiesService() {
        this.properties = new HashMap()
    }

    ApplicationPropertiesService(Map properties) {
        this.properties = properties
    }

    Map getProperties() {
        return properties
    }

    void setProperties(Map properties) {
        this.properties = properties
    }

    void addProperty(String key, String value) {
        this.properties.put(key, value)
    }
}

这是我正在做的测试:

@Test
void "test"() {
    ApplicationPropertiesService  appPropServ = new ApplicationPropertiesService()
    appPropServ.addProperty("test", "property")
    Configuration testConfig = new Configuration()
    testConfig.setApplicationPropertiesService(appPropServ)

    println new JsonBuilder(testConfig).toPrettyString()
}

但是我最终得到的当然是这样的:

{
    "applicationPropertiesService": {
        "properties": {
            "test": "property"
        }
    }
}

所以我不知道如何覆盖该名称,以便在将其解析为 JSON 时,它将由 . 分隔,而不是像现在这样使用驼峰式大小写。 从我一直在阅读的内容来看,来自 jackson.annotation 包的注释 @JsonProperty("application.properties.service") 应该完全符合我的要求,但我尝试将它位于属性的 getter 之前,然后将其放在类开头的属性声明之上,但在这两种情况下,它似乎都忽略了它。

我错过了什么吗?

最佳答案

这不是 JsonProperty 问题,而是代码的问题

Configuration(ApplicationPropertiesService applicationPropertiesService) {
    this.applicationPropertiesService = applicationPropertiesService
}

因此,您的 Configuration 中没有默认构造函数,这使得 jackson 无法创建 Configuration 对象。 (它使用了反射,并且不知 Prop 体使用哪个构造函数。因此它使用默认构造函数)。

尝试添加

Configuration() {

}

Configuration类。

<小时/>

更新:您不应将 Jackson ObjectMapperJsonBuilder 中内置的 Groovy 混合使用。

    ApplicationPropertiesService appPropServ = new ApplicationPropertiesService()
    appPropServ.addProperty("test", "property")
    Configuration testConfig = new Configuration()
    testConfig.setApplicationPropertiesService(appPropServ)
     println new ObjectMapper().writeValueAsString(testConfig)

输出:

{"application.properties.service":{"properties":{"test":"property"}}}

关于java - 将对象转换为 json 时覆盖属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899986/

相关文章:

java - 使用通用父类型输入参数强制执行动态多态调用

java - Java链表实现中的head是什么

java - LibGDX gwd 不继承所需的模块

PHP 文件中的 Javascript 输出 JSON

ios - 使用 Button 快速传递数据

java - 在 hibernate 中使用复合主键保留 1-m 实体

javascript - 根据数据属性的键值选择元素

grails - 如何在Grails Controller 中调用方法而不渲染新 View ?

java - 如何处理Map <String,List>中对List的同步访问?

groovy - java.io.IOException : Stream closed (from groovy)