From 6e66af15a6e7dadf94358ee76e80e88420d61d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Wed, 31 Dec 2025 12:10:56 +0100 Subject: [PATCH] Extract remaining WebMVC configuration snippets Closes gh-36088 --- .../modules/ROOT/pages/web/webmvc-cors.adoc | 79 +------------------ .../ROOT/pages/web/webmvc-functional.adoc | 76 +----------------- .../mvc-controller/ann-requestmapping.adoc | 48 +---------- .../ROOT/pages/web/websocket/fallback.adoc | 42 +--------- .../web/mvccorsglobal/WebConfiguration.java | 41 ++++++++++ .../MyConfiguration.java | 48 +++++++++++ .../UserHandler.java | 25 ++++++ .../web/webmvcfnrunning/WebConfiguration.java | 59 ++++++++++++++ .../WebSocketConfiguration.java | 41 ++++++++++ .../WebSocketConfiguration.java | 44 +++++++++++ .../web/mvccorsglobal/WebConfiguration.kt | 40 ++++++++++ .../MyConfiguration.kt | 44 +++++++++++ .../UserHandler.kt | 25 ++++++ .../web/webmvcfnrunning/WebConfiguration.kt | 54 +++++++++++++ .../WebSocketConfiguration.kt | 40 ++++++++++ .../WebSocketConfiguration.kt | 42 ++++++++++ .../web/mvccorsglobal/WebConfiguration.xml | 28 +++++++ 17 files changed, 539 insertions(+), 237 deletions(-) create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/mvccorsglobal/WebConfiguration.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.java create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/mvccorsglobal/WebConfiguration.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.kt create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.kt create mode 100644 framework-docs/src/main/resources/org/springframework/docs/web/mvccorsglobal/WebConfiguration.xml diff --git a/framework-docs/modules/ROOT/pages/web/webmvc-cors.adoc b/framework-docs/modules/ROOT/pages/web/webmvc-cors.adoc index 6a165d09bad..3a3c0a52db8 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc-cors.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc-cors.adoc @@ -286,84 +286,9 @@ the `allowOriginPatterns` property may be used to match to a dynamic set of orig `maxAge` is set to 30 minutes. -[[mvc-cors-global-java]] -=== Java Configuration -[.small]#xref:web/webflux-cors.adoc#webflux-cors-global[See equivalent in the Reactive stack]# - -To enable CORS in the MVC Java config, you can use the `CorsRegistry` callback, -as the following example shows: - -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableWebMvc - public class WebConfig implements WebMvcConfigurer { - - @Override - public void addCorsMappings(CorsRegistry registry) { - - registry.addMapping("/api/**") - .allowedOrigins("https://domain2.com") - .allowedMethods("PUT", "DELETE") - .allowedHeaders("header1", "header2", "header3") - .exposedHeaders("header1", "header2") - .allowCredentials(true).maxAge(3600); - - // Add more mappings... - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableWebMvc - class WebConfig : WebMvcConfigurer { - - override fun addCorsMappings(registry: CorsRegistry) { - - registry.addMapping("/api/**") - .allowedOrigins("https://domain2.com") - .allowedMethods("PUT", "DELETE") - .allowedHeaders("header1", "header2", "header3") - .exposedHeaders("header1", "header2") - .allowCredentials(true).maxAge(3600) - - // Add more mappings... - } - } ----- -====== - -[[mvc-cors-global-xml]] -=== XML Configuration - -To enable CORS in the XML namespace, you can use the `` element, -as the following example shows: - -[source,xml,indent=0,subs="verbatim"] ----- - - - - - - - ----- +You can enable CORS in the Spring MVC configuration as the following example shows: +include-code::./WebConfiguration[tag=snippet,indent=0] [[mvc-cors-filter]] == CORS Filter diff --git a/framework-docs/modules/ROOT/pages/web/webmvc-functional.adoc b/framework-docs/modules/ROOT/pages/web/webmvc-functional.adoc index 1b3097b691a..6afd34536a7 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc-functional.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc-functional.adoc @@ -900,81 +900,9 @@ processing lifecycle and also (potentially) run side by side with annotated cont any are declared. It is also how functional endpoints are enabled by the Spring Boot Web starter. -The following example shows a WebMvc Java configuration: +The following example shows a related Spring MVC configuration: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableMvc - public class WebConfig implements WebMvcConfigurer { - - @Bean - public RouterFunction routerFunctionA() { - // ... - } - - @Bean - public RouterFunction routerFunctionB() { - // ... - } - - // ... - - @Override - public void configureMessageConverters(List> converters) { - // configure message conversion... - } - - @Override - public void addCorsMappings(CorsRegistry registry) { - // configure CORS... - } - - @Override - public void configureViewResolvers(ViewResolverRegistry registry) { - // configure view resolution for HTML rendering... - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableMvc - class WebConfig : WebMvcConfigurer { - - @Bean - fun routerFunctionA(): RouterFunction<*> { - // ... - } - - @Bean - fun routerFunctionB(): RouterFunction<*> { - // ... - } - - // ... - - override fun configureMessageConverters(converters: List>) { - // configure message conversion... - } - - override fun addCorsMappings(registry: CorsRegistry) { - // configure CORS... - } - - override fun configureViewResolvers(registry: ViewResolverRegistry) { - // configure view resolution for HTML rendering... - } - } ----- -====== +include-code::./WebConfiguration[tag=snippet,indent=0] [[webmvc-fn-handler-filter-function]] diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-requestmapping.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-requestmapping.adoc index f89c79e48aa..654c1e9b6bf 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-requestmapping.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-requestmapping.adoc @@ -550,53 +550,7 @@ You can programmatically register handler methods, which you can use for dynamic registrations or for advanced cases, such as different instances of the same handler under different URLs. The following example registers a handler method: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - public class MyConfig { - - @Autowired - public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler) // <1> - throws NoSuchMethodException { - - RequestMappingInfo info = RequestMappingInfo - .paths("/user/{id}").methods(RequestMethod.GET).build(); // <2> - - Method method = UserHandler.class.getMethod("getUser", Long.class); // <3> - - mapping.registerMapping(info, handler, method); // <4> - } - } ----- -<1> Inject the target handler and the handler mapping for controllers. -<2> Prepare the request mapping meta data. -<3> Get the handler method. -<4> Add the registration. - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - @Configuration - class MyConfig { - - @Autowired - fun setHandlerMapping(mapping: RequestMappingHandlerMapping, handler: UserHandler) { // <1> - val info = RequestMappingInfo.paths("/user/{id}").methods(RequestMethod.GET).build() // <2> - val method = UserHandler::class.java.getMethod("getUser", Long::class.java) // <3> - mapping.registerMapping(info, handler, method) // <4> - } - } ----- -<1> Inject the target handler and the handler mapping for controllers. -<2> Prepare the request mapping meta data. -<3> Get the handler method. -<4> Add the registration. -====== +include-code::./MyConfiguration[tag=snippet,indent=0] diff --git a/framework-docs/modules/ROOT/pages/web/websocket/fallback.adoc b/framework-docs/modules/ROOT/pages/web/websocket/fallback.adoc index 86b979a76b4..bdbac08ae32 100644 --- a/framework-docs/modules/ROOT/pages/web/websocket/fallback.adoc +++ b/framework-docs/modules/ROOT/pages/web/websocket/fallback.adoc @@ -152,26 +152,9 @@ from the iframe. By default, the iframe is set to download the SockJS client from a CDN location. It is a good idea to configure this option to use a URL from the same origin as the application. -The following example shows how to do so in Java configuration: +The following example shows how to configure it: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - @EnableWebSocketMessageBroker - public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { - - @Override - public void registerStompEndpoints(StompEndpointRegistry registry) { - registry.addEndpoint("/portfolio").withSockJS() - .setClientLibraryUrl("http://localhost:8080/myapp/js/sockjs-client.js"); - } - - // ... - - } ----- - -The XML namespace provides a similar option through the `` element. +include-code::./WebSocketConfiguration[tag=snippet,indent=0] NOTE: During initial development, do enable the SockJS client `devel` mode that prevents the browser from caching SockJS requests (like the iframe) that would otherwise @@ -307,23 +290,4 @@ jettyHttpClient.setExecutor(new QueuedThreadPool(1000)); The following example shows the server-side SockJS-related properties (see javadoc for details) that you should also consider customizing: -[source,java,indent=0,subs="verbatim,quotes"] ----- - @Configuration - public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport { - - @Override - public void registerStompEndpoints(StompEndpointRegistry registry) { - registry.addEndpoint("/sockjs").withSockJS() - .setStreamBytesLimit(512 * 1024) <1> - .setHttpMessageCacheSize(1000) <2> - .setDisconnectDelay(30 * 1000); <3> - } - - // ... - } ----- -<1> Set the `streamBytesLimit` property to 512KB (the default is 128KB -- `128 * 1024`). -<2> Set the `httpMessageCacheSize` property to 1,000 (the default is `100`). -<3> Set the `disconnectDelay` property to 30 property seconds (the default is five seconds --- `5 * 1000`). +include-code::./WebSocketConfiguration[tag=snippet,indent=0] diff --git a/framework-docs/src/main/java/org/springframework/docs/web/mvccorsglobal/WebConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/web/mvccorsglobal/WebConfiguration.java new file mode 100644 index 00000000000..8d790e4bc3b --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/mvccorsglobal/WebConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-present 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.docs.web.mvccorsglobal; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +// tag::snippet[] +@Configuration +public class WebConfiguration implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/api/**") + .allowedOrigins("https://domain1.com", "https://domain2.com") + .allowedMethods("GET", "PUT") + .allowedHeaders("header1", "header2", "header3") + .exposedHeaders("header1", "header2") + .allowCredentials(true) + .maxAge(3600); + + // Add more mappings... + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.java new file mode 100644 index 00000000000..8c7373a64b4 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-present 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.docs.web.webmvc.mvccontroller.mvcannrequestmappingregistration; + +import java.lang.reflect.Method; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +// tag::snippet[] +@Configuration +public class MyConfiguration { + + // Inject the target handler and the handler mapping for controllers + @Autowired + public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler) + throws NoSuchMethodException { + + // Prepare the request mapping meta data + RequestMappingInfo info = RequestMappingInfo + .paths("/user/{id}").methods(RequestMethod.GET).build(); + + // Get the handler method + Method method = UserHandler.class.getMethod("getUser", Long.class); + + // Add the registration + mapping.registerMapping(info, handler, method); + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.java new file mode 100644 index 00000000000..51d6c7e242e --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.java @@ -0,0 +1,25 @@ +/* + * Copyright 2002-present 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.docs.web.webmvc.mvccontroller.mvcannrequestmappingregistration; + +public class UserHandler { + + public void getUser(Long id) { + // ... + } +} + diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.java new file mode 100644 index 00000000000..b21cd377381 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-present 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.docs.web.webmvcfnrunning; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverters; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.function.RouterFunction; + +// tag::snippet[] +@Configuration +public class WebConfiguration implements WebMvcConfigurer { + + @Bean + public RouterFunction routerFunctionA() { + // ... + return null; + } + + @Bean + public RouterFunction routerFunctionB() { + // ... + return null; + } + + @Override + public void configureMessageConverters(HttpMessageConverters.ServerBuilder builder) { + // configure message conversion... + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + // configure CORS... + } + + @Override + public void configureViewResolvers(ViewResolverRegistry registry) { + // configure view resolution for HTML rendering... + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.java new file mode 100644 index 00000000000..9ba6f8bfc18 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright 2002-present 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.docs.web.websocket.websocketfallbacksockjsclient; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurationSupport; + +// tag::snippet[] +@Configuration +public class WebSocketConfiguration extends WebSocketMessageBrokerConfigurationSupport { + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/sockjs").withSockJS() + // Set the streamBytesLimit property to 512KB (the default is 128KB -- 128 * 1024) + .setStreamBytesLimit(512 * 1024) + // Set the httpMessageCacheSize property to 1,000 (the default is 100) + .setHttpMessageCacheSize(1000) + // Set the disconnectDelay property to 30 property seconds (the default is five seconds -- 5 * 1000) + .setDisconnectDelay(30 * 1000); + } + + // ... +} +// end::snippet[] + diff --git a/framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.java b/framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.java new file mode 100644 index 00000000000..85df5e29781 --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.java @@ -0,0 +1,44 @@ +/* + * Copyright 2002-present 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.docs.web.websocket.websocketfallbackxhrvsiframe; + +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; + +// tag::snippet[] +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer { + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/portfolio").withSockJS() + .setClientLibraryUrl("http://localhost:8080/myapp/js/sockjs-client.js"); + } + + // ... + + @Override + public void configureMessageBroker(MessageBrokerRegistry registry) { + // Configure message broker... + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/mvccorsglobal/WebConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/mvccorsglobal/WebConfiguration.kt new file mode 100644 index 00000000000..cfa74d13254 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/mvccorsglobal/WebConfiguration.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-present 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.docs.web.mvccorsglobal + +import org.springframework.context.annotation.Configuration +import org.springframework.web.servlet.config.annotation.CorsRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +// tag::snippet[] +@Configuration +class WebConfiguration : WebMvcConfigurer { + + override fun addCorsMappings(registry: CorsRegistry) { + registry.addMapping("/api/**") + .allowedOrigins("https://domain1.com", "https://domain2.com") + .allowedMethods("GET", "PUT") + .allowedHeaders("header1", "header2", "header3") + .exposedHeaders("header1", "header2") + .allowCredentials(true) + .maxAge(3600) + + // Add more mappings... + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.kt new file mode 100644 index 00000000000..c4b0f50155d --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/MyConfiguration.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2002-present 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.docs.web.webmvc.mvccontroller.mvcannrequestmappingregistration + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.annotation.Configuration +import org.springframework.web.bind.annotation.RequestMethod +import org.springframework.web.servlet.mvc.method.RequestMappingInfo +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping + +// tag::snippet[] +@Configuration +class MyConfiguration { + + // Inject the target handler and the handler mapping for controllers + @Autowired + fun setHandlerMapping(mapping: RequestMappingHandlerMapping, handler: UserHandler) { + + // Get the handler method + val info = RequestMappingInfo.paths("/user/{id}").methods(RequestMethod.GET).build() + + // Get the handler method + val method = UserHandler::class.java.getMethod("getUser", Long::class.java) + + // Add the registration + mapping.registerMapping(info, handler, method) + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.kt new file mode 100644 index 00000000000..84d71aa68c9 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvccontroller/mvcannrequestmappingregistration/UserHandler.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2002-present 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.docs.web.webmvc.mvccontroller.mvcannrequestmappingregistration + +class UserHandler { + + fun getUser(id: Long) { + // ... + } +} + diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.kt new file mode 100644 index 00000000000..c0d1d0cb57b --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvcfnrunning/WebConfiguration.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2002-present 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.docs.web.webmvcfnrunning + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.converter.HttpMessageConverters +import org.springframework.web.servlet.config.annotation.CorsRegistry +import org.springframework.web.servlet.config.annotation.ViewResolverRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer +import org.springframework.web.servlet.function.RouterFunction + +// tag::snippet[] +@Configuration +class WebConfiguration : WebMvcConfigurer { + + @Bean + fun routerFunctionA(): RouterFunction<*> { + TODO() + } + + @Bean + fun routerFunctionB(): RouterFunction<*> { + TODO() + } + + override fun configureMessageConverters(builder: HttpMessageConverters.ServerBuilder) { + TODO() + } + + override fun addCorsMappings(registry: CorsRegistry) { + TODO() + } + + override fun configureViewResolvers(registry: ViewResolverRegistry) { + TODO() + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.kt new file mode 100644 index 00000000000..d89b6cc3806 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbacksockjsclient/WebSocketConfiguration.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2002-present 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.docs.web.websocket.websocketfallbacksockjsclient + +import org.springframework.context.annotation.Configuration +import org.springframework.web.socket.config.annotation.StompEndpointRegistry +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurationSupport + +// tag::snippet[] +@Configuration +class WebSocketConfiguration : WebSocketMessageBrokerConfigurationSupport() { + + override fun registerStompEndpoints(registry: StompEndpointRegistry) { + registry.addEndpoint("/sockjs").withSockJS() + // Set the streamBytesLimit property to 512KB (the default is 128KB -- 128 * 1024) + .setStreamBytesLimit(512 * 1024) + // Set the httpMessageCacheSize property to 1,000 (the default is 100) + .setHttpMessageCacheSize(1000) + // Set the disconnectDelay property to 30 property seconds (the default is five seconds -- 5 * 1000) + .setDisconnectDelay(30 * 1000) + } + + // ... +} +// end::snippet[] + diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.kt new file mode 100644 index 00000000000..c8b86e91ac2 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/websocket/websocketfallbackxhrvsiframe/WebSocketConfiguration.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2002-present 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.docs.web.websocket.websocketfallbackxhrvsiframe + +import org.springframework.context.annotation.Configuration +import org.springframework.messaging.simp.config.MessageBrokerRegistry +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker +import org.springframework.web.socket.config.annotation.StompEndpointRegistry +import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer + +// tag::snippet[] +@Configuration +@EnableWebSocketMessageBroker +class WebSocketConfiguration : WebSocketMessageBrokerConfigurer { + + override fun registerStompEndpoints(registry: StompEndpointRegistry) { + registry.addEndpoint("/portfolio").withSockJS() + .setClientLibraryUrl("http://localhost:8080/myapp/js/sockjs-client.js") + } + + // ... + + override fun configureMessageBroker(registry: MessageBrokerRegistry) { + // Configure message broker... + } +} +// end::snippet[] + diff --git a/framework-docs/src/main/resources/org/springframework/docs/web/mvccorsglobal/WebConfiguration.xml b/framework-docs/src/main/resources/org/springframework/docs/web/mvccorsglobal/WebConfiguration.xml new file mode 100644 index 00000000000..66244af74ef --- /dev/null +++ b/framework-docs/src/main/resources/org/springframework/docs/web/mvccorsglobal/WebConfiguration.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + +