java Jackson @JsonPropertyOrder 被忽略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33186004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:18:57  来源:igfitidea点击:

Hymanson @JsonPropertyOrder is ignored

javajsonHymanson

提问by Alviere

I'm currently developing RESTful app with Spark framework and I use Hymanson for serialization. And I encountered such issue: @JsonPropertyOrder is ignored.

我目前正在使用 Spark 框架开发 RESTful 应用程序,我使用 Hymanson 进行序列化。我遇到了这样的问题:@JsonPropertyOrder 被忽略。

Here's my POJO:

这是我的 POJO:

@Data
@JsonPropertyOrder({"id", "company", "title", "infos", "startDate", "endDate"})
public class Info {
    @JsonProperty("id")
    long id;

    @JsonProperty("company")
    Company company;

    @JsonProperty("title")
    String title;

    @JsonProperty("infos")
    List<Prize> infos;

    @JsonProperty("startDate")
    Date startDate;

    @JsonProperty("endDate")
    Date endDate;

    public Info() {}
}

I generate JSON with this method:

我用这个方法生成JSON:

public static String generateJSONResponse(Object response) {
    if (responseObjectMapper == null) {
        responseObjectMapper = new ObjectMapper(new JsonFactory());
        responseObjectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    }

    try {
        return responseObjectMapper.writeValueAsString(response);
    } catch (IOException ioe) {
        // Must not occur
    }

    // Something really unexpected happened, so we return unknown response;
    return ErrorMessages.ERROR_RESPONSE_UNKNOWN;
}

And in the end I receive this:

最后我收到了这个:

{
  "status": 0,
  "result": {
    "infoList": [
      {
        "infos": [...],
        "id": 2,
        "title": "...",
        "company": {...},
        "startDate": 1445238000000,
        "endDate": 1445792400000
      },
      ...
    ]
  }
}

What am I doing wrong? Or something is wrong with Hymanson? Can anybody help me with that?

我究竟做错了什么?或者Hyman逊有什么问题?有人可以帮我吗?

Also, I tried this using Hymanson version 2.6.3 and 2.3.5. Both works the same way.

另外,我使用 Hymanson 版本 2.6.3 和 2.3.5 尝试了这个。两者的工作方式相同。

回答by Ashwin Jayaprakash

The annotation JavaDoc also echoes the "unordered" collection: http://fasterxml.github.io/Hymanson-annotations/javadoc/2.3.0/com/fasterxml/Hymanson/annotation/JsonPropertyOrder.html

注释 JavaDoc 也呼应了“无序”集合:http: //fasterxml.github.io/Hymanson-annotations/javadoc/2.3.0/com/fasterxml/Hymanson/annotation/JsonPropertyOrder.html

This annotation may or may not have effect on deserialization: for basic JSON handling there is no effect, but for other supported data types (or structural conventions) there may be.

此注释可能对反序列化有影响,也可能没有影响:对于基本的 JSON 处理没有影响,但对于其他支持的数据类型(或结构约定)可能有影响。

That probably explains why it is not working.

这可能解释了为什么它不起作用。

回答by Nitin Jain

Lomboks @Data interferes with Hymanson deserializing. Remove this and try..

Lomboks @Data 干扰 Hymanson 反序列化。删除这个并尝试..