I have configured spring cloud gateway in my spring boot project. My requirement is that, i want to change query params values in Custom written org.springframework.cloud.gateway.filter.GlobalFilter .
e.g postman request - http://localhost:8080/employee/message?q1=abc&q2=123
expected filter redirection - http://localhost:8080/employee/message?q1=xyz&q2=789
so that, my controller will get q1 value as xyz and q2 value as 789 instead the original values. So i want to change the value of my query params as per business case. I am able to get the query params in my filter but not able to figure out exactly how to change them.
Following is my custom filter
@Component
public class PreFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerWebExchange modExchange = changeQueryParams(exchange);
return chain.filter(modExchange);
}
private static ServerWebExchange changeQueryParams(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
Map<String, List<String>> map = new HashMap<>();
map.put("q1", Arrays.asList("xyz"));
map.put("q2", Arrays.asList("789"));
MultiValueMap<String, String> newMultiValueMap = new LinkedMultiValueMap<>(map);
Builder builder = request.mutate();
request = builder.uri(UriComponentsBuilder.fromUri(exchange.getRequest()
.getURI())
.replaceQueryParams(newMultiValueMap)
.build()
.toUri()).build();
ServerWebExchange modifiedExchange = exchange.mutate().request(request).build();
return modifiedExchange;
}
}
Spring boot version = 2.7.0