在下面的类中,我将方法引用WordCounterEx::accumulate作为第二个参数传递给reduce方法.reduce方法的签名是:
<U> U reduce(U identity,
BiFunction<U, ? super T, U> accumulator,
BinaryOperator<U> combiner);
Run Code Online (Sandbox Code Playgroud)
因此reduce方法的第二个参数必须满足BiFunction配方.但是传递的累积方法不是BiFunction(它只有一个参数).为什么还要编译?
public class WordCounterEx {
private final int counter;
private final boolean lastSpace;
public WordCounterEx(int counter, boolean lastSpace) {
this.counter = counter;
this.lastSpace = lastSpace;
}
public int countWords(Stream<Character> stream) {
WordCounterEx wordCounter = stream.reduce(new WordCounterEx(0, true),
//HOW CAN THIS WORK? here must come BiFunction - R apply(T t, U u);
WordCounterEx::accumulate,
WordCounterEx::combine);
return wordCounter.counter;
}
public WordCounterEx accumulate(Character c) {
if(Character.isWhitespace(c)) {
return lastSpace …Run Code Online (Sandbox Code Playgroud) 我需要对403 Forbidden HTTP状态(获取/刷新)JWT令牌做出反应(在拦截器类中)并使用新令牌重试请求.
在下面的代码中,当服务器返回错误响应时,它会转到成功回调(而不是我期望的错误回调),并且事件是typeof对象(对错误响应没有反应).事件对象如下所示:{type:0}.
题:
- 当我需要刷新accessToken并重试http请求时,如何在HttpInterceptor中正确处理httpErrorResponse(403 Forbidden)?
import {
HttpInterceptor,
HttpRequest,
HttpResponse,
HttpHandler,
HttpEvent
} from '@angular/common/http';
import 'rxjs/add/operator/map';
@Injectable()
class JWTInterceptor implements HttpInterceptor {
constructor(private tokenService: TokenService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let myHeaders = req.headers;
if (this.tokenService.accessToken) {
myHeaders = myHeaders.append('Authorization',`${this.tokenService.accessToken.token_type} ${this.tokenService.accessToken.access_token}`)
}
const authReq = req.clone({headers: myHeaders});
return next.handle(authReq).map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// success callback
}
}, (err: any) => {
if (err instanceof HttpErrorResponse {
if (err.status === …Run Code Online (Sandbox Code Playgroud)