RESTful API - Response Filtering

Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/terawarehouse-react Wi...


Slack Discussion

Repositories
With our category entity, most of the time we will just use the fields code, description, and entityId. And to return a response filtered like that we will need a class from jackson-databind jar. This dependency should already be added to your project from spring-boot-starter-web or spring-boot-data-rest.

And this is how we do it.

[open CategoryController class, filteredCategory method]
Examining this method, we can see that the category object is wrapped inside a categoryResource class. It does the same thing as our categoryResourceAssembler so yes it’s another alternative.

[open CategoryResource class]
Take note that we defined a JSON_Filter string constant here.

But really the main focus of this lecture is about the bean property filter which filters out all the fields of the category class except the ones we specified. And wrapping this class inside a MappingJacksonValue which we return as the response. This approach is really powerful as we can send the fields that we need in the request parameter as a set of strings. Here we can, change the filters into code only or maybe code and entityId. Note that this approach works for both single and list of entities.

Run the application

[run the postman test]

Finally, I’ll be presenting another approach to return an entity response and that is by using a JsonView.

[open CategoryView class]
Normally, in an application, we show a specific set of fields for a given role. For simplicity let’s just say that we have Public for a promoter and Internal for a supervisor. And yes all fields accessible through public must also be accessible through internal as it has higher authority.

The first thing to do is to associate the fields with a view. Let’s check the business entity.

[open BusinessEntity class]
Notice that both code and description are associated with the Public view.

To apply the filter to the rest response we need to annotate the endpoint with @JsonView.

[open CategoryController class, filteredView method]

Run the application.

[run the postman tests]

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: RESTful API - Response Filtering
RESTful API - Response Filtering
https://i.ytimg.com/vi/JeKF0OrTBtA/0.jpg
https://i.ytimg.com/vi/JeKF0OrTBtA/0.jpg
toztech
https://toztech.blogspot.com/2019/08/restful-api-response-filtering.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2019/08/restful-api-response-filtering.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content