Rohan Joseph George
Rohan Joseph George

Reputation: 21

JSON response for REST API

I have made a REST API in java and I have the following 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;
    }

}

In my API class, I get the data for the above DTO from sql and return the response using the code:

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

The output I get looks like this:

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

So my doubts are:

  1. Why does the Object list come in third in the response even though I have declared it last in the DTO?
  2. Why is isTypeTrue and isTypeSpecial shown as TypeTrue and TypeSpecial in the output?
  3. Why has isTypeTrue appeared first and isTypeSpecial appeared afterwards even though I declared isTypeSpecial first?
  4. Is there any way i can learn how the buildResponse works?

Upvotes: 1

Views: 705

Answers (2)

Dilyano Senders
Dilyano Senders

Reputation: 199

  1. This is because the JSONObject implementation uses HashMap by default. HashMap will order it alphabetically. Mostly a JSON response order is irrelevant, if the answer contains the value you need, it is all good.
  2. The JSON framework renames your primitive variables by removing the 'is' and 'has' word from your variable name. You can add a different name by adding @JsonProperty(value="isTypeSpecial")

  3. This answer is stated in answer 1, it is because of the alphabetical order in a HashMap

  4. It is possible with debugging, but I think you don't want to go in debt to much

Hopefully this answers your questions.

Upvotes: 0

luk2302
luk2302

Reputation: 57114

  1. Because the order is irrelevant. On JSON level it basically is a unordered key-value map. The order must not matter to you or the one who parses. Only stuff inside [ ... ] will keep its order since that is an ordered list / array.
  2. boolean fields are a bit special in their getter naming, see e.g. For a boolean field, what is the naming convention for its getter/setter? - the is... is like the get... for regular getters and is therefore stripped away.
  3. same as 1.
  4. yes, e.g. set a breakpoint and step into the method using your IDE + debugger - but it will probably not be fun to look at the code

Upvotes: 1

Related Questions