Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor controller to use functional routing #7809

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package framework.benchmark.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@RestController
public class BenchmarkController {

private static final String EMPTY = "";
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@GetMapping("/")
public Mono<String> root() {
return Mono.just(EMPTY);
}
@Component
public class BenchmarkController {

@GetMapping("/user/{id}")
public Mono<Integer> userId(@PathVariable Integer id) {
return Mono.just(id);
}
private static final Mono<ServerResponse> EMPTY_RESPONSE = ServerResponse.ok().build();

@PostMapping("/user")
public Mono<String> user() {
return Mono.just(EMPTY);
@Bean
public RouterFunction<ServerResponse> routes() {
return route()
.GET("/", request -> EMPTY_RESPONSE)
.GET("/user/{id}", request -> ServerResponse.ok().bodyValue(request.pathVariable("id")))
.POST("/user", request -> EMPTY_RESPONSE)
.build();
}
}
Loading