Skip to content
Advertisement

Java show/hide object fields depending on REST call

Is it possible to config the Item class that depending on the REST call to show and hide specific fields? For example I want to hide colorId (and show categoryId) from User class when calling XmlController and vice versa when calling JsonController.

Item class

JavaScript

JSON Controller

JavaScript

XML Controller

JavaScript

Advertisement

Answer

I’ve solved it by creating following View:

JavaScript

With this config it is possible to set up the Class as:

JavaScript

(Note: if you want to list List<GroupedItem> item in your response you need to define @JsonView(View.Parent.class) in GroupedItem as well)

Finally, if you are using Spring, the REST requests (See question) can be defined as:

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