【问题标题】:Json schema field orderJson 模式字段顺序
【发布时间】:2018-11-06 14:28:35
【问题描述】:

我知道 json 架构对象中列出的字段没有定义的顺序,因为它们不是数组,但我正在寻找一种能够在我的应用程序 UI 中以正确顺序显示它们的方法。

到目前为止,我发现的解决方法包括使用不同的serializer,甚至将数字硬编码到字段名称中。

我想提出一些适用于我当前设置的东西。 Hibernate、Spring Boot 和 react-app 前端。 给定这个 GET 请求:

/profile/personEntities 

带有标题:接受:application/schema+json

我会收到这个:

{
    "title": "Person entity",
    "properties": {
        "birthday": {
            "title": "Birthday",
            "readOnly": false,
            "type": "string",
            "format": "date-time"
        },
        "lastName": {
            "title": "Last name",
            "readOnly": false,
            "type": "string"
        },
        "address": {
            "title": "Address",
            "readOnly": false,
            "type": "string",
            "format": "uri"
        },
        "firstName": {
            "title": "First name",
            "readOnly": false,
            "type": "string"
        },
        "email": {
            "title": "Email",
            "readOnly": false,
            "type": "string"
        },
        "cellPhone": {
            "title": "Cell phone",
            "readOnly": false,
            "type": "string"
        }
    },
    "requiredProperties": [
        "firstName",
        "lastName"
    ],
    "definitions": {},
    "type": "object",
    "$schema": "http://json-schema.org/draft-04/schema#"
}

我尝试将@JsonProperty(index=2) 添加到该字段,但没有任何变化。

非常感谢您提供的任何提示。

【问题讨论】:

    标签: mysql hibernate spring-boot jsonschema


    【解决方案1】:

    如果您使用 Jackson 来处理您的序列化/反序列化,您可以使用 @JsonPropertyOrder - 来自他们的文档:

    // ensure that "id" and "name" are output before other properties
    @JsonPropertyOrder({ "id", "name" })
    
    // order any properties that don't have explicit setting using alphabetic order
    @JsonPropertyOrder(alphabetic=true)
    

    见:http://fasterxml.github.io/jackson-annotations/javadoc/2.3.0/com/fasterxml/jackson/annotation/JsonPropertyOrder.html

    【讨论】:

    • 谢谢。这在我进行标准 Spring Data Rest Call 时有效(例如:/personEntities -> {_embedded: [{firstName:Tester, lastName:McTest}]}),但是当我调用获取架构时(示例在原帖),没有变化。我查看了他们的代码,但我不确定 Spring 是如何处理模式数据的。知道他们如何序列化模式数据吗?
    • 哦,我明白了,很有趣 - 我原以为它也会尊重 @JsonPropertyOrder 注释,因为 Spring 使用 Jackson 来处理他们的 json 序列化/反序列化。我一直在做一些挖掘,似乎没有办法控制模式的序列化顺序 - 请参阅此答案以了解潜在的解决方法:stackoverflow.com/a/31693656/3368558
    猜你喜欢
    • 1970-01-01
    • 2020-06-13
    • 2019-05-24
    • 2019-10-05
    • 2013-07-02
    • 1970-01-01
    • 2022-01-03
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多