-
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
Support for removing JSON attributes from response bodies in MVC #3777
Conversation
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.
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.
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); |
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.
What will happen if this exception is not handled in the configuration as you do in the test?
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 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.
5903a10
to
9ba0e65
Compare
Signed-off-by: raccoonback <[email protected]>
@ryanjbaxter I've changed the target to the 4.2.x branch. |
9ba0e65
to
77dd818
Compare
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