7

我正在使用 Jackson 模块的最新分支 - jackson-module-jsonSchema,即 2.4.4-Snapshot。

我正在尝试使用 @JsonPropertyOrder 注释来维护 POJO 属性的顺序,但它似乎不尊重注释。

我的示例 POJO 如下 -

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"a", "b"})
class Pojo {
    private String a;
    private String b;

    public Pojo(final String a, final String b) {
        this.a = a;
        this.b = b;
    }

    public void setA(final String a) {
        this.a = a;
    }

    public void setB(final String b) {
        this.b = b;
    }

    public String getA() {
        return this.a;
    }

    public String getB() {
        return this.b;
    }
}

杰克逊代码如下 -

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class Test {

    public static void main(String[] args) throws JsonProcessingException {
        // TODO Auto-generated method stub
        final ObjectMapper mapper = new ObjectMapper();
        final SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(mapper.constructType(Pojo.class), visitor);
        final JsonSchema jsonSchema = visitor.finalSchema();            
        System.out.println(mapper.writeValueAsString(jsonSchema));
    }

}

Json输出如下 -

{
    "type": "object",
    "id": "urn:jsonschema:Pojo",
    "properties": {
        "b": {
            "type": "string"
        },
        "a": {
            "type": "string"
        }
    }
}

有人可以建议我是否做错了什么,或者我们是否需要针对杰克逊提出问题,因为我看到此类问题已在 2.3.2 中关闭(https://github.com/FasterXML/jackson-数据格式-xml/issues/91)?

4

1 回答 1

0

使用 @JsonPropertyOrder(alphabetic=true) 这将按字母顺序对所有属性进行排序 适用于 Jackson 1.9.13

于 2016-04-26T18:19:04.597 回答