ServerResponse represents an HTTP response in Spring’s functional reactive API, including its headers and body. A request handler returns one, often by building an OK response and attaching a reactive body. section 12.2
Returning a Hello World response
A route matching GET /hello can return ServerResponse.ok().body(...), producing HTTP 200 with a "Hello World!" payload. The body is supplied through Reactor’s Mono.just. section 12.2
package hello;
import static org.springframework.web
.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web
.reactive.function.server.RouterFunctions.route;
import static org.springframework.web
.reactive.function.server.ServerResponse.ok;
import static reactor.core.publisher.Mono.just;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
@Configuration
public class RouterFunctionConfig {
@Bean
public RouterFunction<?> helloRouterFunction() {
return route(GET("/hello"),
request -> ok().body(just("Hello World!"), String.class))
;
}
}
Inspect a complete functional route configuration that imports ServerResponse.ok, matches GET /hello, and returns a reactive string body. section 12.2
