Skip to content

Commit 096ba72

Browse files
committed
Add documentation for WebMvc.fn
See gh-29683
1 parent 430d510 commit 096ba72

File tree

5 files changed

+104
-7
lines changed

5 files changed

+104
-7
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/developing-web-applications.adoc

+15-1
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@ The following code shows a typical `@RestController` that serves JSON data:
2222
include::{docs-java}/features/developingwebapplications/springmvc/MyRestController.java[]
2323
----
2424

25+
"`WebMvc.fn`", the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example:
26+
27+
[source,java,indent=0,subs="verbatim"]
28+
----
29+
include::{docs-java}/features/developingwebapplications/springmvc/MyRoutingConfiguration.java[]
30+
----
31+
32+
[source,java,indent=0,subs="verbatim"]
33+
----
34+
include::{docs-java}/features/developingwebapplications/springmvc/MyUserHandler.java[]
35+
----
36+
2537
Spring MVC is part of the core Spring Framework, and detailed information is available in the {spring-framework-docs}/web.html#mvc[reference documentation].
2638
There are also several guides that cover Spring MVC available at https://spring.io/guides.
2739

28-
40+
TIP: You can define as many `RouterFunction` beans as you like to modularize the definition of the router.
41+
Beans can be ordered if you need to apply a precedence.
2942

3043
[[features.developing-web-applications.spring-mvc.auto-configuration]]
3144
==== Spring MVC Auto-configuration
45+
3246
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
3347

3448
The auto-configuration adds the following features on top of Spring's defaults:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.developingwebapplications.springmvc;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.http.MediaType;
22+
import org.springframework.web.servlet.function.RequestPredicate;
23+
import org.springframework.web.servlet.function.RouterFunction;
24+
import org.springframework.web.servlet.function.ServerResponse;
25+
26+
import static org.springframework.web.servlet.function.RequestPredicates.accept;
27+
import static org.springframework.web.servlet.function.RouterFunctions.route;
28+
29+
@Configuration(proxyBeanMethods = false)
30+
public class MyRoutingConfiguration {
31+
32+
private static final RequestPredicate ACCEPT_JSON = accept(MediaType.APPLICATION_JSON);
33+
34+
@Bean
35+
public RouterFunction<ServerResponse> routerFunction(MyUserHandler userHandler) {
36+
// @formatter:off
37+
return route()
38+
.GET("/{user}", ACCEPT_JSON, userHandler::getUser)
39+
.GET("/{user}/customers", ACCEPT_JSON, userHandler::getUserCustomers)
40+
.DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser)
41+
.build();
42+
// @formatter:on
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.developingwebapplications.springmvc;
18+
19+
import org.springframework.stereotype.Component;
20+
import org.springframework.web.servlet.function.ServerRequest;
21+
import org.springframework.web.servlet.function.ServerResponse;
22+
23+
@Component
24+
public class MyUserHandler {
25+
26+
public ServerResponse getUser(ServerRequest request) {
27+
/**/ return ServerResponse.ok().build();
28+
}
29+
30+
public ServerResponse getUserCustomers(ServerRequest request) {
31+
/**/ return ServerResponse.ok().build();
32+
}
33+
34+
public ServerResponse deleteUser(ServerRequest request) {
35+
/**/ return ServerResponse.ok().build();
36+
}
37+
38+
}

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/developingwebapplications/springwebflux/MyRoutingConfiguration.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.springframework.web.reactive.function.server.RouterFunction;
2424
import org.springframework.web.reactive.function.server.ServerResponse;
2525

26-
import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE;
27-
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
2826
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
2927
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
3028

@@ -36,10 +34,11 @@ public class MyRoutingConfiguration {
3634
@Bean
3735
public RouterFunction<ServerResponse> monoRouterFunction(MyUserHandler userHandler) {
3836
// @formatter:off
39-
return route(
40-
GET("/{user}").and(ACCEPT_JSON), userHandler::getUser).andRoute(
41-
GET("/{user}/customers").and(ACCEPT_JSON), userHandler::getUserCustomers).andRoute(
42-
DELETE("/{user}").and(ACCEPT_JSON), userHandler::deleteUser);
37+
return route()
38+
.GET("/{user}", ACCEPT_JSON, userHandler::getUser)
39+
.GET("/{user}/customers", ACCEPT_JSON, userHandler::getUserCustomers)
40+
.DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser)
41+
.build();
4342
// @formatter:on
4443
}
4544

src/checkstyle/checkstyle-suppressions.xml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<suppress files="[\\/]spring-boot-docs[\\/]" checks="JavadocType|JavadocVariable" />
2121
<suppress files="[\\/]spring-boot-docs[\\/]" checks="SpringJavadoc" message="\@since" />
2222
<suppress files="[\\/]spring-boot-docs[\\/].*jooq" checks="AvoidStaticImport" />
23+
<suppress files="[\\/]spring-boot-docs[\\/].*MyRoutingConfiguration\.java" checks="AvoidStaticImport"/>
2324
<suppress files="[\\/]spring-boot-smoke-tests[\\/]" checks="JavadocType" />
2425
<suppress files="[\\/]spring-boot-smoke-tests[\\/]" checks="ImportControl" />
2526
<suppress files="[\\/]spring-boot-smoke-tests[\\/]" id="mainCodeIllegalImportCheck" />

0 commit comments

Comments
 (0)