Home > Mobile >  Spring boot Controller json response has a field name "empty"
Spring boot Controller json response has a field name "empty"

Time:03-12

I have a GET endpoint in my application supposed to return :

{
  "gameId": "41a483c4-6220-424a-a931-d9114a4f6748",
  "pits": [
    {
      "id": 1,
      "stones": 6
    },
    {
      "id": 2,
      "stones": 6
    },
    {
      "id": 3,
      "stones": 6
    },
    {
      "id": 4,
      "stones": 6
    },
    {
      "id": 5,
      "stones": 6
    },
    {
      "id": 6,
      "stones": 6
    },
    {
      "id": 7,
      "stones": 0
    },
    {
      "id": 8,
      "stones": 6
    },
    {
      "id": 9,
      "stones": 6
    },
    {
      "id": 10,
      "stones": 6
    },
    {
      "id": 11,
      "stones": 6
    },
    {
      "id": 12,
      "stones": 6
    },
    {
      "id": 13,
      "stones": 6
    },
    {
      "id": 14,
      "stones": 0
    }
  ],
  "playerTurn": null,
  "currentPitIndex": 0
}

but instead it returns:

{
    "id": "25f09303-b797-418f-a7e7-db0e5fa8631b",
    "pits": [
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 0,
            "empty": true
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 6,
            "empty": false
        },
        {
            "stones": 0,
            "empty": true
        }
    ],
    "playerTurn": null,
    "currentPitIndex": null
}

I am wondering what is "empty"?! and where is "id"!

would be much appreciated for any suggestion and help. thank you

CodePudding user response:

Does your class have a method called isEmpty()? Because most JSON marshalling frameworks add JSON properties for any method with 0 arguments that starts with get, or returns a boolean and starts with is. For the same reason id is probably missing - I'm guessing there is no getter for it. The top level gameId is probably called id because you have a getId() method, not a getGameId() method.

For most frameworks you can tweak this with annotations. For instance, with Jackon you can use @JsonIgnore to indicate a method should not be represented as a JSON property, and @JsonProperty can be used to a) add a custom name, or b) allow a field to be included as well. For other frameworks you should check their documentation.

  • Related