Skip to content

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

Conversation

raccoonback
Copy link
Contributor

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

  • Implemented a filter that removes selected JSON fields in Spring MVC responses.
  • Reused core logic from the WebFlux implementation where possible to maintain consistency.

@raccoonback raccoonback force-pushed the support-json-field-removal-filter-in-mvc branch from 736a0b8 to 5903a10 Compare April 19, 2025 15:08
Comment on lines +183 to +192
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);
}
Copy link
Contributor Author

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())) {
Copy link
Contributor Author

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.

Comment on lines +203 to +205
if (deleteRecursively) {
jsonNode.forEach(childNode -> removeJsonAttributes(childNode, fieldNames, true));
}
Copy link
Contributor Author

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.

Copy link
Contributor

@ryanjbaxter ryanjbaxter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also go in 4.2.x?

responseBody = OBJECT_MAPPER.writeValueAsString(jsonBodyContent);
}
catch (JsonProcessingException exception) {
throw new IllegalStateException("Failed to process JSON of response body.", exception);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if this exception is not handled in the configuration as you do in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanjbaxter

If the global exception is not handled as in the test configuration, the request will not be routed to a specific handler.
Instead, the DispatcherServlet will forward the request to the /error path for default error handling.

@raccoonback raccoonback force-pushed the support-json-field-removal-filter-in-mvc branch from 5903a10 to 9ba0e65 Compare May 13, 2025 00:00
@raccoonback raccoonback changed the base branch from main to 4.2.x May 13, 2025 00:00
@raccoonback
Copy link
Contributor Author

@ryanjbaxter I've changed the target to the 4.2.x branch.

@raccoonback raccoonback requested a review from ryanjbaxter May 13, 2025 00:23
@raccoonback raccoonback force-pushed the support-json-field-removal-filter-in-mvc branch from 9ba0e65 to 77dd818 Compare May 13, 2025 00:24
@ryanjbaxter ryanjbaxter added this to the 4.2.3 milestone May 13, 2025
@ryanjbaxter ryanjbaxter merged commit 66da5c7 into spring-cloud:4.2.x May 13, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this to Done in 2025.0.0 May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants