@Override public Mono<ServerResponse> handle(ServerRequest request) { HttpMethod method = request.method(); if (method != null) { switch (method) { case GET: return EntityResponse.fromObject(this.resource).build() .map(response -> response); case HEAD: Resource headResource = new HeadMethodResource(this.resource); return EntityResponse.fromObject(headResource).build() .map(response -> response); case OPTIONS: return ServerResponse.ok() .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); } } return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); }
@Test public void allow() { Mono<ServerResponse> result = ServerResponse.ok().allow(HttpMethod.GET).build(); Set<HttpMethod> expected = EnumSet.of(HttpMethod.GET); StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getAllow())) .expectComplete() .verify(); }
@Override public Mono<ServerResponse> handle(ServerRequest request) { switch (request.method()) { case GET: return ServerResponse.ok() .body(BodyInserters.fromResource(this.resource)); case HEAD: Resource headResource = new HeadMethodResource(this.resource); return ServerResponse.ok() .body(BodyInserters.fromResource(headResource)); case OPTIONS: return ServerResponse.ok() .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); default: return ServerResponse.status(HttpStatus.METHOD_NOT_ALLOWED) .allow(SUPPORTED_METHODS) .body(BodyInserters.empty()); } }