Skip to content
Advertisement

Rest API ResponseEntity add extra attribute to response

I’m trying to figure out response of a GET method API, it is returning extra attribute with name “attributesMap”

Reponse entity return code is return ResponseEntity.ok(document);

document model class

JavaScript

but the API reponse JSON as an extra attribute “attributesMap”. Sample JSON as below:

   {
      "id": "xxx",
       "format": "xx",
       "attributes": [
    {
      "name": "attr1",
      "values": [
        "val1"
      ]
    }
  ],
  "type": "test type",
  "accessright": "DELETE",
  "attributesMap": {
    "name": "attr1",
      "values": [
        "val1"
      ]
    }
    }

Can anyone help me figure out how to check where this attribute coming from , when I debugged till the return return ResponseEntity.ok(document); there is no attributesMap in the document model object.

Advertisement

Answer

The problem in your case is that you have the getter getAttributesMap. What will jackson serialize by default?

  1. All public fields
  2. All getter methods

What can you do?

  1. Rename the method
  2. Exclude the getter with @JsonIgnore
JavaScript

You could also provide your own ObjectMapper bean with the following settings:

JavaScript

or

JavaScript

This would only serialize fields, not getters.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement