Servlet-filter traverse/modify JSON response body

Hi all, hope someone can help me out,

I got a servlet filter that more or less works the way I want it to. I can get the response body, remove some nasty characters and do stuff I want. Most of the time, when I debug the content of the response it’s HTML and that I can work with. However, when it’s a rest resource, I get an empty string in when I do log.debug of it’s content.

Is getting JSON from the response a bit different than getting HTML? In the end my plan is to actually parse the JSON into JSONObject or something so that I can process it, get data from it and populate it with additional data base no the data that’s already in there.

The thing is, this is a rest resource of another plugin, and if someone has a few pointes that would be much appreciated.

Thanks

1 Like

Hello!

As a first look, it’s not the best idea to put a servlet-filter on another plugin REST; there’s probably a better way to reach your functional needs; maybe create a new REST API

As for the servlet filter, it’s just on the way you are reading the response; for me it should be something like this:
create your JSON Object by creating a class for it with the correct implementation (parameters etc) - that by reading the response using httpServletResponse.getCharacterEncoding();
Amend your object with the changes you need and get back the JSON data from the object; clean your response and write back into it using httpServletResponse.getWriter().write("json here");

2 Likes