【问题标题】:JSON response for REST APIREST API 的 JSON 响应
【发布时间】:2019-02-21 03:11:57
【问题描述】:

我在 java 中制作了一个 REST API,并且我有以下 DTO。

@ApiModel(value = "testType", description = "Test type")
public class TestType
{
    private int type;
    private String typeName;
    private boolean isTypeSpecial;
    private boolean isTypeTrue;

    @JsonInclude(JsonInclude.Include.NON_ABSENT)
    private List<typeMasterDTO> typeMasterList;

    public TestType()
    {
    }

    @ApiModelProperty(example = "0", value = "Type", required = true)
    public int getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type= type;
    }

    @ApiModelProperty(example = "Dragon", value = "Name of the typw",             required = true)
    public String getTypeName()
    {
        return typeName;
    }

    public void setTypeName(String typeName)
    {
        this.typeName= typeName;
    }

    @ApiModelProperty(value = "It is a special type", required = true)
    public boolean isTypeSpecial()
    {
        return isTypeSpecial;
    }

    public void setTypeSpecial(boolean isTypeSpecial)
    {
        this.isTypeSpecial= isTypeSpecial;
    }

    @ApiModelProperty(value = "It is a true type", required = true)
    public boolean isTypeTrue()
    {
        return isTypeTrue;
    }

    public void setTrueType(boolean isTypeTrue)
    {
        this.isTypeTrue= isTypeTrue;
    }

    @ApiModelProperty(value = "List of types")
    public List<typeMasterDTO> getTypeMasterList()
    {
        return typeMasterList;
    }

    public void setTypeMasterList(List<typeMasterDTO> typeMasterList)
    {
        this.typeMasterList= typeMasterList;
    }

}

在我的 API 类中,我从 sql 获取上述 DTO 的数据并使用代码返回响应:

Response com.mmp.rest.AbstractResource.buildResponse(Response<?> response, ResponseMode mode)

我得到的输出是这样的:

[
      {
            "type": 1,
            "typeName": "New type",
            "typeMasterList": [
                {
                    "typeMaster": 0,
                    "typeMasterName": "Default"
                },
                {
                    "typeMaster": 1,
                    "typemasterName": "Custom"
                }
            ],
            "TypeTrue": false,
            "TypeSpecial": true
    }
]

所以我的疑问是:

  1. 为什么对象列表在响应中排在第三位,即使我已在 DTO 中最后声明它?
  2. 为什么 isTypeTrue 和 isTypeSpecial 在输出中显示为 TypeTrue 和 TypeSpecial?
  3. 为什么我先声明了isTypeSpecial,却先出现isTypeTrue,后出现isTypeSpecial?
  4. 有什么方法可以了解 buildResponse 的工作原理吗?

【问题讨论】:

    标签: java json rest swagger


    【解决方案1】:
    1. 因为顺序无关紧要。在 JSON 级别上,它基本上是一个无序的键值映射。顺序对您或解析的人来说一定无关紧要。只有 [ ... ] 中的内容会保持其顺序,因为这是一个有序列表/数组。
    2. 布尔字段在它们的 getter 命名上有点特殊,参见例如For a boolean field, what is the naming convention for its getter/setter? - is... 就像普通 getter 的 get... 一样,因此被剥离了。
    3. 同 1。
    4. 是的,例如使用 IDE + 调试器设置断点并单步执行该方法 - 但查看代码可能不会很有趣

    【讨论】:

    • 哇,那是准确和信息丰富的......谢谢但是,我尝试调试到响应生成器并进入“找不到源......Jar文件没有源附件”页面。
    • @RohanJosephGeorge 你用的是什么IDE?
    • 我正在使用eclipse
    【解决方案2】:
    1. 这是因为 JSONObject 实现默认使用 HashMap。 HashMap 将按字母顺序排列。大多数情况下,JSON 响应顺序是无关紧要的,如果答案包含您需要的值,那就很好。
    2. JSON 框架通过从变量名中删除“is”和“has”字样来重命名原始变量。您可以通过添加 @JsonProperty(value="isTypeSpecial")

    3. 来添加不同的名称
    4. 这个答案在答案1中说明,这是因为HashMap中的字母顺序

    5. 调试是可能的,但我认为你不想负债太多

    希望这能回答您的问题。

    【讨论】:

    • 是的,这确实回答了我的问题。 @JsonProperty 是绕过它的好方法,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2014-09-04
    • 1970-01-01
    • 2017-01-21
    • 2014-09-01
    相关资源
    最近更新 更多