2

I have an object in spring in which i would like to hide fields while using @ responsebody and display the fields back when i want to return using @responseentity.

I tried @jsonignore on setter and @jsonproperty on getter... it doesn't work...

I tried @jsonproperty(access=Read.only) On fields i wanted to hide...it doesn't work...

I tried @jsonignoreproperties ... it doesn't work

Is there a way to do it...rather than creating 2 objects

Please help... thanks

goks
  • 1,196
  • 3
  • 18
  • 37
Adam
  • 31
  • 4
  • 1
    Not sure you can do it solely with those annotations, but for what it's worth you can do the opposite(exclude a property from deserialization) as detailed in http://www.davismol.net/2015/03/21/jackson-using-jsonignore-and-jsonproperty-annotations-to-exclude-a-property-only-from-json-deserialization/ – Jean Marois May 18 '18 at 02:10

2 Answers2

0

You cant Hide fields in the object by using @responsebody and display it by using @responseentity.

If you want to send any additional data using HTTPResponse headers then we can use @Responseentity. Please refer this link to see the difference between these 2 annotations.

To actually answer your question we can create custom annotation and use it in controller to hide fields in the response. Refer this Github project for implementation

Justin
  • 17
  • 4
0

You can create a custom serializer and use it directly with ObjectMapper or apply it to the class using @JsonSerialize, short tutorial: http://www.baeldung.com/jackson-custom-serialization

Jean Marois
  • 1,510
  • 11
  • 19