SpringCloudGateWay自定义自己的GateWayFilterFactory的时候(exchange, chain)
Java8 函数式接口在SpringCloudGateWay上的应用
·
在看SpringCloudGateWay的时候,在实现父类的GatewayFilter apply(C config)方法的时候怎么看也没看明白:
package com.quantex.scg;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import java.util.List;
public class WsSecurityGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return (exchange, chain) -> {
List<String> strings = exchange.getRequest().getHeaders().get(config.getName());
if (strings!=null){
ServerHttpRequest request = exchange.getRequest().mutate()
.header(config.getName(), strings.get(0))
.build();
// System.out.println("-----------------------------------------");
// System.out.println(strings.get(0));
return chain.filter(exchange.mutate().request(request).build());
}else{
return chain.filter(exchange);
}
};
}
}
怎么也没看明白这个函数式的写法(exchange, chain)->{} 不知道到底这exchange和chain是怎么来的,也没看到全局变量啊,我寻思着难道是继承的父类的全局变量?我就一层层找,也灭有找到,实在想不明白,我还以为是容器托管的有名字为exchange和chain的实例,想破脑袋,但是觉得不可能,后来我看了一下apply()这个函数的返回值,是一个GateWayFilter,我就点进去看了一下这个源码如下:
package org.springframework.cloud.gateway.filter;
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.springframework.cloud.gateway.support.ShortcutConfigurable;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* Contract for interception-style, chained processing of Web requests that may
* be used to implement cross-cutting, application-agnostic requirements such
* as security, timeouts, and others. Specific to a Gateway
*
* Copied from WebFilter
*
* @author Rossen Stoyanchev
* @since 5.0
*/
public interface GatewayFilter extends ShortcutConfigurable {
String NAME_KEY = "name";
String VALUE_KEY = "value";
/**
* Process the Web request and (optionally) delegate to the next
* {@code WebFilter} through the given {@link GatewayFilterChain}.
* @param exchange the current server exchange
* @param chain provides a way to delegate to the next filter
* @return {@code Mono<Void>} to indicate when request processing is complete
*/
Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);
}
结果我恍然大悟,这尼玛不是JAVA8的新操作?当接口只有一个没有实现的方法时,如果返回的是一个函数,其实就时返回了这个接口的是一个实现类的实例,对应的实现方法的逻辑就是那个函数,这里的GatewayFilter 的filter方法的实现其实就是:
(exchange, chain) -> {
List<String> strings = exchange.getRequest().getHeaders().get(config.getName());
if (strings!=null){
ServerHttpRequest request = exchange.getRequest().mutate()
.header(config.getName(), strings.get(0))
.build();
// System.out.println("-----------------------------------------");
// System.out.println(strings.get(0));
return chain.filter(exchange.mutate().request(request).build());
}else{
return chain.filter(exchange);
}
};
同时(exchange,chain)也刚好对上了接口里的filter的两个参数ServerWebExchange exchange, GatewayFilterChain chain
综上:这个其实就是JAVA8的函数式接口, 知道java8的特性,但是操作的少。才会出现这种知道但是看不懂的情况,其实函数式接口很简单,下面的链接3分钟就能看明白.
更多推荐
已为社区贡献5条内容
所有评论(0)