-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Support for removing JSON attributes from response bodies in MVC #3777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Support for removing JSON attributes from response bodies in MVC #3777
Conversation
Signed-off-by: raccoonback <[email protected]>
736a0b8
to
5903a10
Compare
try { | ||
JsonNode jsonBodyContent = OBJECT_MAPPER.readValue(responseBody, JsonNode.class); | ||
|
||
removeJsonAttributes(jsonBodyContent, immutableFieldList, deleteRecursively); | ||
|
||
responseBody = OBJECT_MAPPER.writeValueAsString(jsonBodyContent); | ||
} | ||
catch (JsonProcessingException exception) { | ||
throw new IllegalStateException("Failed to process JSON of response body.", exception); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove a specific attribute based on a JsonNode
.
|
||
return modifyResponseBody(String.class, String.class, APPLICATION_JSON_VALUE, (request, response, body) -> { | ||
String responseBody = body; | ||
if (APPLICATION_JSON.isCompatibleWith(response.headers().getContentType())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only process when the content-type is application/json
.
if (deleteRecursively) { | ||
jsonNode.forEach(childNode -> removeJsonAttributes(childNode, fieldNames, true)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If nested attributes also need to be removed, handle them recursively.
Motivation
This PR adds support for filtering out specific JSON attributes from response bodies in Spring MVC, extending functionality previously available only in WebFlux.
Changes