diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc index bf9a527dc8c..fb1d19b585c 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc @@ -12,13 +12,21 @@ The following code shows a typical `@RestController` that serves JSON data: include::code:MyRestController[] +"`WebMvc.fn`", the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example: + +include::code:MyRoutingConfiguration[] + +include::code:MyUserHandler[] + Spring MVC is part of the core Spring Framework, and detailed information is available in the {spring-framework-docs}/web.html#mvc[reference documentation]. There are also several guides that cover Spring MVC available at https://spring.io/guides. - +TIP: You can define as many `RouterFunction` beans as you like to modularize the definition of the router. +Beans can be ordered if you need to apply a precedence. [[web.servlet.spring-mvc.auto-configuration]] ==== Spring MVC Auto-configuration + Spring Boot provides auto-configuration for Spring MVC that works well with most applications. The auto-configuration adds the following features on top of Spring's defaults: diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/MyRoutingConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/MyRoutingConfiguration.java index d0dbee00c57..4ff9304e3ff 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/MyRoutingConfiguration.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/reactive/webflux/MyRoutingConfiguration.java @@ -23,8 +23,6 @@ import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerResponse; -import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE; -import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RequestPredicates.accept; import static org.springframework.web.reactive.function.server.RouterFunctions.route; @@ -36,10 +34,11 @@ public class MyRoutingConfiguration { @Bean public RouterFunction monoRouterFunction(MyUserHandler userHandler) { // @formatter:off - return route( - GET("/{user}").and(ACCEPT_JSON), userHandler::getUser).andRoute( - GET("/{user}/customers").and(ACCEPT_JSON), userHandler::getUserCustomers).andRoute( - DELETE("/{user}").and(ACCEPT_JSON), userHandler::deleteUser); + return route() + .GET("/{user}", ACCEPT_JSON, userHandler::getUser) + .GET("/{user}/customers", ACCEPT_JSON, userHandler::getUserCustomers) + .DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser) + .build(); // @formatter:on } diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/MyRoutingConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/MyRoutingConfiguration.java new file mode 100644 index 00000000000..866821760c2 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/MyRoutingConfiguration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.web.servlet.springmvc; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; +import org.springframework.web.servlet.function.RequestPredicate; +import org.springframework.web.servlet.function.RouterFunction; +import org.springframework.web.servlet.function.ServerResponse; + +import static org.springframework.web.servlet.function.RequestPredicates.accept; +import static org.springframework.web.servlet.function.RouterFunctions.route; + +@Configuration(proxyBeanMethods = false) +public class MyRoutingConfiguration { + + private static final RequestPredicate ACCEPT_JSON = accept(MediaType.APPLICATION_JSON); + + @Bean + public RouterFunction routerFunction(MyUserHandler userHandler) { + // @formatter:off + return route() + .GET("/{user}", ACCEPT_JSON, userHandler::getUser) + .GET("/{user}/customers", ACCEPT_JSON, userHandler::getUserCustomers) + .DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser) + .build(); + // @formatter:on + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/MyUserHandler.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/MyUserHandler.java new file mode 100644 index 00000000000..404ef7a3608 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/web/servlet/springmvc/MyUserHandler.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.web.servlet.springmvc; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.function.ServerRequest; +import org.springframework.web.servlet.function.ServerResponse; + +@Component +public class MyUserHandler { + + public ServerResponse getUser(ServerRequest request) { + /**/ + return ServerResponse.ok().build(); + } + + public ServerResponse getUserCustomers(ServerRequest request) { + /**/ + return ServerResponse.ok().build(); + } + + public ServerResponse deleteUser(ServerRequest request) { + /**/ + return ServerResponse.ok().build(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/MyRoutingConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/MyRoutingConfiguration.kt new file mode 100644 index 00000000000..dedc069ab21 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/MyRoutingConfiguration.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.web.servlet.springmvc + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.MediaType +import org.springframework.web.servlet.function.RequestPredicates.accept +import org.springframework.web.servlet.function.RouterFunction +import org.springframework.web.servlet.function.RouterFunctions +import org.springframework.web.servlet.function.ServerResponse + +@Configuration(proxyBeanMethods = false) +class MyRoutingConfiguration { + + @Bean + fun routerFunction(userHandler: MyUserHandler): RouterFunction { + return RouterFunctions.route() + .GET("/{user}", ACCEPT_JSON, userHandler::getUser) + .GET("/{user}/customers", ACCEPT_JSON, userHandler::getUserCustomers) + .DELETE("/{user}", ACCEPT_JSON, userHandler::deleteUser) + .build() + } + + companion object { + private val ACCEPT_JSON = accept(MediaType.APPLICATION_JSON) + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/MyUserHandler.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/MyUserHandler.kt new file mode 100644 index 00000000000..9695030c97a --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/web/servlet/springmvc/MyUserHandler.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.web.servlet.springmvc + +import org.springframework.stereotype.Component +import org.springframework.web.servlet.function.ServerRequest +import org.springframework.web.servlet.function.ServerResponse + +@Suppress("UNUSED_PARAMETER") +@Component +class MyUserHandler { + + fun getUser(request: ServerRequest?): ServerResponse { + return ServerResponse.ok().build() + } + + fun getUserCustomers(request: ServerRequest?): ServerResponse { + return ServerResponse.ok().build() + } + + fun deleteUser(request: ServerRequest?): ServerResponse { + return ServerResponse.ok().build() + } + +} diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index 37f25362031..16d5f0d188e 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -20,6 +20,7 @@ +