我正在使用 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;
}
}
Jackson代码如下-
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"
}
}
}
有人可以建议我是否做错了什么,或者我们是否需要针对 jackson 提出问题,因为我看到此类问题已在 2.3.2 ( https://github.com/FasterXML/jackson-dataformat-xml/issues/91 ) 中关闭?
最佳答案
使用 @JsonPropertyOrder(字母=真) 这将按字母顺序排列所有属性 适用于 Jackson 1.9.13
关于java - @JsonPropertyOrder 不适用于 jackson-module-jsonSchema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26884345/