Spring-cloud-gateway: Can I write response body before setComplete?

Created on 29 Mar 2018  路  3Comments  路  Source: spring-cloud/spring-cloud-gateway

I hava a customer filter, I want to return specified content such as "Hello World", but I can only get response status code 502 without response content. Where is wrong?

@Component
public class CustomerFilter implements GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        byte[] bytes = "Hello World".getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
        exchange.getResponse().writeWith(Flux.just(buffer));
        exchange.getResponse().setStatusCode(HttpStatus.BAD_GATEWAY);
        return exchange.getResponse().setComplete();
    }
}

Most helpful comment

If I don't call exchange.getResponse().setComplete() , it works, I get response body "Hello World",
can anyone explain it ?

@Component
public class CustomerFilter implements GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        byte[] bytes = "Hello World".getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
        exchange.getResponse().setStatusCode(HttpStatus.BAD_GATEWAY);
        return exchange.getResponse().writeWith(Flux.just(buffer));
    }
}

All 3 comments

If I don't call exchange.getResponse().setComplete() , it works, I get response body "Hello World",
can anyone explain it ?

@Component
public class CustomerFilter implements GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        byte[] bytes = "Hello World".getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
        exchange.getResponse().setStatusCode(HttpStatus.BAD_GATEWAY);
        return exchange.getResponse().writeWith(Flux.just(buffer));
    }
}

The manual is here and here.
Spring cloud gateway is not based on spring MVC but on spring webflux. Or, in short, there is no need to call setComplete manually.

If I don't call exchange.getResponse().setComplete() , it works, I get response body "Hello World",
can anyone explain it ?

@Component
public class CustomerFilter implements GatewayFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        byte[] bytes = "Hello World".getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
        exchange.getResponse().setStatusCode(HttpStatus.BAD_GATEWAY);
        return exchange.getResponse().writeWith(Flux.just(buffer));
    }
}

thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zjengjie picture zjengjie  路  6Comments

gaoyf picture gaoyf  路  3Comments

vborcea picture vborcea  路  6Comments

samtonyclarke picture samtonyclarke  路  3Comments

aresa7796 picture aresa7796  路  6Comments