liveBook · Manning

Serverresponse

As explained in Spring in Action, Sixth Edition

A Spring functional web API type representing an HTTP response, including its headers and body.

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

How Spring in Action, Sixth Edition covers it

How deep it goes
Demonstrates construction through ok() and body(), but does not detail broader response-building options or internals.
Examples
Uses a GET /hello route that returns HTTP 200 with a "Hello World!" body.
What you can do after
Build it from scratch by returning an OK ServerResponse with a reactive body from a functional route.
How it connects
Connects ServerResponse with ServerRequest, RequestPredicate, RouterFunction, and reactive types such as Mono.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.