Skip to content

Commit 391752a

Browse files
committed
Polish and update reactive getting started reference
This commit updates the instructions on getting started with Spring Web Reactive and also updates constructors and setters to streamline the getting started procedure. Issue: SPR-14640
1 parent 436486b commit 391752a

File tree

8 files changed

+67
-50
lines changed

8 files changed

+67
-50
lines changed

spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
7171
private List<HandlerResultHandler> resultHandlers;
7272

7373

74+
public DispatcherHandler() {
75+
}
76+
77+
public DispatcherHandler(ApplicationContext applicationContext) {
78+
initStrategies(applicationContext);
79+
}
80+
81+
7482
@Override
7583
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
7684
initStrategies(applicationContext);

spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public void setUp() throws Exception {
8787
appContext.register(TestConfig.class);
8888
appContext.refresh();
8989

90-
this.dispatcherHandler = new DispatcherHandler();
91-
this.dispatcherHandler.setApplicationContext(appContext);
90+
this.dispatcherHandler = new DispatcherHandler(appContext);
9291

9392
this.request = new MockServerHttpRequest(HttpMethod.GET, new URI("/"));
9493
MockServerHttpResponse response = new MockServerHttpResponse();

spring-web-reactive/src/test/java/org/springframework/web/reactive/result/SimpleUrlHandlerMappingIntegrationTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ protected HttpHandler createHttpHandler() {
6161
wac.register(WebConfig.class);
6262
wac.refresh();
6363

64-
DispatcherHandler dispatcherHandler = new DispatcherHandler();
65-
dispatcherHandler.setApplicationContext(wac);
66-
67-
return WebHttpHandlerBuilder.webHandler(dispatcherHandler)
64+
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac))
6865
.exceptionHandlers(new ResponseStatusExceptionHandler())
6966
.build();
7067
}

spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/AbstractRequestMappingIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public abstract class AbstractRequestMappingIntegrationTests extends AbstractHtt
4444
@Override
4545
protected HttpHandler createHttpHandler() {
4646
this.applicationContext = initApplicationContext();
47-
DispatcherHandler handler = new DispatcherHandler();
48-
handler.setApplicationContext(this.applicationContext);
49-
return WebHttpHandlerBuilder.webHandler(handler).build();
47+
return WebHttpHandlerBuilder
48+
.webHandler(new DispatcherHandler(this.applicationContext))
49+
.build();
5050
}
5151

5252
protected abstract ApplicationContext initApplicationContext();

spring-web-reactive/src/test/java/org/springframework/web/reactive/result/method/annotation/SseIntegrationTests.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ protected HttpHandler createHttpHandler() {
6565
this.wac.register(TestConfiguration.class);
6666
this.wac.refresh();
6767

68-
DispatcherHandler webHandler = new DispatcherHandler();
69-
webHandler.setApplicationContext(this.wac);
70-
71-
return WebHttpHandlerBuilder.webHandler(webHandler).build();
68+
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(this.wac)).build();
7269
}
7370

7471
@Test

spring-web/src/main/java/org/springframework/http/server/reactive/UndertowHttpHandlerAdapter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.reactivestreams.Subscription;
2424

2525
import org.springframework.core.io.buffer.DataBufferFactory;
26+
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
2627
import org.springframework.util.Assert;
2728

2829
/**
@@ -40,17 +41,20 @@ public class UndertowHttpHandlerAdapter implements io.undertow.server.HttpHandle
4041

4142
private final HttpHandler delegate;
4243

43-
private final DataBufferFactory dataBufferFactory;
44+
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(false);
4445

4546

46-
public UndertowHttpHandlerAdapter(HttpHandler delegate, DataBufferFactory dataBufferFactory) {
47+
public UndertowHttpHandlerAdapter(HttpHandler delegate) {
4748
Assert.notNull(delegate, "'delegate' is required");
48-
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null");
4949
this.delegate = delegate;
50-
this.dataBufferFactory = dataBufferFactory;
5150
}
5251

5352

53+
public void setDataBufferFactory(DataBufferFactory dataBufferFactory) {
54+
Assert.notNull(dataBufferFactory, "'dataBufferFactory' must not be null");
55+
this.dataBufferFactory = dataBufferFactory;
56+
}
57+
5458
@Override
5559
public void handleRequest(HttpServerExchange exchange) throws Exception {
5660

spring-web/src/test/java/org/springframework/http/server/reactive/bootstrap/UndertowHttpServer.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import io.undertow.Undertow;
2020
import io.undertow.server.HttpHandler;
2121

22-
import org.springframework.core.io.buffer.DataBufferFactory;
23-
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
2422
import org.springframework.http.server.reactive.UndertowHttpHandlerAdapter;
2523
import org.springframework.util.Assert;
2624

@@ -31,19 +29,13 @@ public class UndertowHttpServer extends HttpServerSupport implements HttpServer
3129

3230
private Undertow server;
3331

34-
private DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
35-
3632
private boolean running;
3733

38-
public void setDataBufferFactory(DataBufferFactory dataBufferFactory) {
39-
this.dataBufferFactory = dataBufferFactory;
40-
}
4134

4235
@Override
4336
public void afterPropertiesSet() throws Exception {
4437
Assert.notNull(getHttpHandler());
45-
HttpHandler handler =
46-
new UndertowHttpHandlerAdapter(getHttpHandler(), dataBufferFactory);
38+
HttpHandler handler = new UndertowHttpHandlerAdapter(getHttpHandler());
4739
this.server = Undertow.builder().addHttpListener(getPort(), getHost())
4840
.setHandler(handler).build();
4941
}

src/asciidoc/web-reactive.adoc

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,47 +153,67 @@ Single<Account> response = webClient
153153
[[web-reactive-getting-started-boot]]
154154
=== Spring Boot Starter
155155

156-
The experimental Spring Boot Web Reactive starter available via http://start.spring.io
157-
is the quickest way to get started. It does all the work so you can start
156+
The
157+
https://github.com/bclozel/spring-boot-web-reactive#spring-boot-web-reactive-starter[Spring Boot Web Reactive starter]
158+
available via http://start.spring.io
159+
is the fastest way to get started. It does all that's necessary so you can start
158160
writing `@Controller` classes. By default it runs on Tomcat but the dependencies can
159161
be changed as usual with Spring Boot to switch to a different runtime.
160162

161163

162164
[[web-reactive-getting-started-manual]]
163165
=== Manual Bootstrapping
164166

165-
It is also easy to get started by writing a few lines of code:
167+
This section outlines the steps to get up and running without Spring Boot.
166168

169+
For dependencies start with `spring-web-reactive` and `spring-context`.
170+
Then add `jackson-databind` and `io.netty:netty-buffer:4.1.3.Final`
171+
(temporarily see https://jira.spring.io/browse/SPR-14528[SPR-14528]) for JSON support.
172+
Lastly add the dependencies for one of the supported runtimes:
173+
174+
* Tomcat -- `org.apache.tomcat.embed:tomcat-embed-core`
175+
* Jetty -- `org.eclipse.jetty:jetty-server` and `org.eclipse.jetty:jetty-servlet`
176+
* Reactor Netty -- `io.projectreactor.ipc:reactor-netty`
177+
* RxNetty -- `io.reactivex:rxnetty-common` and `io.reactivex:rxnetty-http`
178+
* Undertow -- `io.undertow:undertow-core`
179+
180+
For the bootstrap code start with:
167181
[source,java,indent=0]
168182
[subs="verbatim,quotes"]
169183
----
170-
AnnotationConfigApplicationContext context;
171-
context = new AnnotationConfigApplicationContext();
184+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
172185
context.register(WebReactiveConfiguration.class); // (1)
173186
context.refresh();
174187
175-
DispatcherHandler handler = new DispatcherHandler(); // (2)
176-
handler.setApplicationContext(context);
177-
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(handler).build();
178-
179-
HttpServer server = new TomcatHttpServer(); // (3)
180-
server.setPort(8080);
181-
server.setHandler(httpHandler);
182-
server.afterPropertiesSet();
183-
server.start();
188+
DispatcherHandler dispatcherHandler = new DispatcherHandler(context); // (2)
189+
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(dispatcherHandler).build();
184190
----
185191

186-
The `WebReactiveConfiguration` at (1) is the Java config from `spring-web-reactive`
187-
and is similar in purpose to the MVC Java config from `spring-webmvc`. It provides the
188-
web framework configuration required to get started leaving you only to
189-
declare your own `@Controller` beans.
190-
191-
The `DispatcherHandler` at (2) is the equivalent of the `DispatcherServlet` in Spring MVC.
192+
The above loads default Spring Web Reactive config (1), then creates a
193+
`DispatcherHandler`, the main class driving request processing (2), and adapts
194+
it to `HttpHandler`, the lowest level Spring abstraction for reactive HTTP request handling.
195+
An `HttpHandler` can then be installed in each supported runtime:
192196

193-
The `HttpServer` at (3) is an abstraction from the
194-
https://github.com/spring-projects/spring-framework/tree/master/spring-web/src/test/java/org/springframework/http/server/reactive/bootstrap[test sources]
195-
of `spring-web-reactive` used for Spring Framework's own integration tests.
196-
The abstraction comes with basic implementations for each supported runtime.
197+
[source,java,indent=0]
198+
[subs="verbatim,quotes"]
199+
----
200+
// Tomcat and Jetty
201+
HttpServlet servlet = new ServletHttpHandlerAdapter(httpHandler);
202+
...
203+
204+
// Reactor Netty
205+
HttpServer server = HttpServer.create(host, port)
206+
server.startAndAwait(new ServletHttpHandlerAdapter(httpHandler));
207+
208+
// RxNetty
209+
HttpServer server = HttpServer.newServer(new InetSocketAddress(host, port))
210+
server.startAndAwait(new RxNettyHttpHandlerAdapter(httpHandler));
211+
212+
// Undertow
213+
Undertow.builder().addHttpListener(port, host)
214+
.setHandler(new UndertowHttpHandlerAdapter(httpHandler))
215+
.build()
216+
----
197217

198218

199219
[[web-reactive-getting-started-M1]]

0 commit comments

Comments
 (0)