Skip to content

Commit 44ff29c

Browse files
committed
Merge branch '2.5.x' into 2.6.x
Closes gh-29728
2 parents f672a20 + 59799c8 commit 44ff29c

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,27 @@ The following code shows a typical `@RestController` that serves JSON data:
1515
include::{docs-java}/web/servlet/springmvc/MyRestController.java[]
1616
----
1717

18+
"`WebMvc.fn`", the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example:
19+
20+
[source,java,indent=0,subs="verbatim"]
21+
----
22+
include::{docs-java}/web/servlet/springmvc/MyRoutingConfiguration.java[]
23+
----
24+
25+
[source,java,indent=0,subs="verbatim"]
26+
----
27+
include::{docs-java}/web/servlet/springmvc/MyUserHandler.java[]
28+
----
29+
1830
Spring MVC is part of the core Spring Framework, and detailed information is available in the {spring-framework-docs}/web.html#mvc[reference documentation].
1931
There are also several guides that cover Spring MVC available at https://spring.io/guides.
2032

21-
33+
TIP: You can define as many `RouterFunction` beans as you like to modularize the definition of the router.
34+
Beans can be ordered if you need to apply a precedence.
2235

2336
[[web.servlet.spring-mvc.auto-configuration]]
2437
==== Spring MVC Auto-configuration
38+
2539
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.
2640

2741
The auto-configuration adds the following features on top of Spring's defaults:

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/MyRoutingConfiguration.java

Lines changed: 5 additions & 6 deletions
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

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.web.servlet.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,41 @@
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.web.servlet.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+
/**/
28+
return ServerResponse.ok().build();
29+
}
30+
31+
public ServerResponse getUserCustomers(ServerRequest request) {
32+
/**/
33+
return ServerResponse.ok().build();
34+
}
35+
36+
public ServerResponse deleteUser(ServerRequest request) {
37+
/**/
38+
return ServerResponse.ok().build();
39+
}
40+
41+
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
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)