spring-cloud-gateway
spring-cloud-gateway copied to clipboard
Implement ModifyResponseBody in MVC Server
Is your feature request related to a problem? Please describe. The org.springframework.web.servlet.function.ServerResponse doesn't provide a way to access response body and mutate it
Describe the solution you'd like Similar to BodyFilterFunctions.modifyRequestBody looking for a way to mutate the response body
Describe alternatives you've considered One option would be using servlet filter but it does not provide easy configuration either declaratively using application properties, yaml or through DSL
Additional context
Hi, I have the similar requirements with mrinmoykhamrui, modifying response body in spring cloud gateway mvc. But I couldn't find the filter to modify response body in mvc and other way to modify ServerResponse body.
According to MVC Gateway Server PR, providing ModifyResponseBody filter is in todo list but not yet provided.
So, until ModifyResponseBody filter is provided, I guess using custom ProxyExchange bean can modify response body.
Default ProxyExchange bean is registered by ConditionalOnMissingBean(ProxyExchange.class) so if I create custom ProxyExchange class which can modify response body and then register bean then I think it's possible to use custom ProxyExchange instead of default ProxyExchange and change response body.
I wonder providing ModifyResponseBody filter is still in plan.
Also waiting for this feature. We would actually really like to use the MVC-based gateway at one of our clients. Sadly the missing modifyResponseBody function, prevents us from using it at the moment.
Hi, I have the similar requirements with mrinmoykhamrui, modifying response body in spring cloud gateway mvc. But I couldn't find the filter to modify response body in mvc and other way to modify ServerResponse body.
According to MVC Gateway Server PR, providing ModifyResponseBody filter is in todo list but not yet provided.
So, until ModifyResponseBody filter is provided, I guess using custom
ProxyExchangebean can modify response body. DefaultProxyExchangebean is registered byConditionalOnMissingBean(ProxyExchange.class)so if I create customProxyExchangeclass which can modify response body and then register bean then I think it's possible to use customProxyExchangeinstead of defaultProxyExchangeand change response body.I wonder providing ModifyResponseBody filter is still in plan.
Have you tried this solution? Any luck?
Hi, We have requirement to modify request body for a json field, and team wants to use MVC if possible. Looks like we can't use custom after filter as ServerResponse does not provide way to access response body and mutate. Is there workaround for this? Likely not if I understand above discussion, please confirm.
We have requirement to modify request body for a json field, and team wants to use MVC if possible.
I had the same requirement. If this is the only thing holding you back from using Spring Cloud Gateway MVC, I would suggest building a classic servlet filter for now, registering it for the appropriate URL you want to do rewrites and do the work there. When they finish the response body rewrite functionality in server-mvc, you can remove your custom servlet filter and switch over to using a gateway filter.
See https://github.com/spring-cloud/spring-cloud-gateway/commit/a54297bd55013dc0055ceaa26290bf32bc217204