This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Cloud Gateway 4.1.5!

SetStatus Filter

The SetStatus filter takes a single parameter, status. It must be a valid Spring HttpStatus. It may be the integer value 404 or the string representation of the enumeration: NOT_FOUND. The following listing configures a SetStatus filter:

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: setstatusstring_route
          uri: https://example.org
          filters:
          - SetStatus=UNAUTHORIZED
        - id: setstatusint_route
          uri: https://example.org
          filters:
          - SetStatus=401
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.AfterFilterFunctions.setStatus;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsSetStatus() {
        return route("setstatus_route")
                .GET("/path1", http("https://example.org"))
                // setStatus("UNAUTHORIZED") works as well
                .after(setStatus(HttpStatus.UNAUTHORIZED))
            .build().and(route("setstatusint_route")
                .GET("/path2", http("https://example.org"))
                .after(setStatus("401"))
            .build());
    }
}

In either case, the HTTP status of the response is set to 401.