Add remove json attributes filter (#2742)

* Add RemoveJsonAttributesFilterFactory
Takes a collection of attribute names to search for to remove from a JSON response, an optional last parameter from the list can be a boolean to remove the attributes just at root level or recursively

* Add docs for RemoveJsonAttributes filter

* Extract a single instance of Json mapper

* Overload boolean parameter to avoid parsing a string
This commit is contained in:
Marta Medio
2022-10-05 17:39:48 +02:00
committed by GitHub
parent 253eec53a8
commit a487e64bfd
5 changed files with 335 additions and 0 deletions

View File

@@ -1118,6 +1118,51 @@ spring:
This will send a status 302 with a `Location:https://acme.org` header to perform a redirect.
=== `RemoveJsonAttributesResponseBody` `GatewayFilter` Factory
The `RemoveJsonAttributesResponseBody` `GatewayFilter` factory takes a collection of `attribute names` to search for, an optional last parameter from the list can be a boolean to remove the attributes just at root level (that's the default value if not present at the end of the parameter configuration, `false`) or recursively (`true`).
It provides a convenient method to apply a transformation to JSON body content by deleting attributes from it.
The following example configures an `RemoveJsonAttributesResponseBody` `GatewayFilter`:
.application.yml
====
[source,yaml]
----
spring:
cloud:
gateway:
routes:
- id: removejsonattributes_route
uri: https://example.org
filters:
- RemoveJsonAttributesResponseBody=id,color
----
====
This removes attributes "id" and "color" from the JSON content body at root level.
The following example configures an `RemoveJsonAttributesResponseBody` `GatewayFilter` that uses the optional last parameter:
.application.yml
====
[source,yaml]
----
spring:
cloud:
gateway:
routes:
- id: removejsonattributes_recursively_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- RemoveJsonAttributesResponseBody=id,color,true
----
====
This removes attributes "id" and "color" from the JSON content body at any level.
=== The `RemoveRequestHeader` GatewayFilter Factory
The `RemoveRequestHeader` `GatewayFilter` factory takes a `name` parameter.