Skip to content

Commit 7939fb5

Browse files
committed
Extract remaining WebMVC configuration snippets
Closes spring-projectsgh-36088
1 parent cd8e384 commit 7939fb5

17 files changed

Lines changed: 539 additions & 237 deletions

File tree

framework-docs/modules/ROOT/pages/web/webmvc-cors.adoc

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -286,84 +286,9 @@ the `allowOriginPatterns` property may be used to match to a dynamic set of orig
286286

287287
`maxAge` is set to 30 minutes.
288288

289-
[[mvc-cors-global-java]]
290-
=== Java Configuration
291-
[.small]#xref:web/webflux-cors.adoc#webflux-cors-global[See equivalent in the Reactive stack]#
292-
293-
To enable CORS in the MVC Java config, you can use the `CorsRegistry` callback,
294-
as the following example shows:
295-
296-
[tabs]
297-
======
298-
Java::
299-
+
300-
[source,java,indent=0,subs="verbatim,quotes"]
301-
----
302-
@Configuration
303-
@EnableWebMvc
304-
public class WebConfig implements WebMvcConfigurer {
305-
306-
@Override
307-
public void addCorsMappings(CorsRegistry registry) {
308-
309-
registry.addMapping("/api/**")
310-
.allowedOrigins("https://domain2.com")
311-
.allowedMethods("PUT", "DELETE")
312-
.allowedHeaders("header1", "header2", "header3")
313-
.exposedHeaders("header1", "header2")
314-
.allowCredentials(true).maxAge(3600);
315-
316-
// Add more mappings...
317-
}
318-
}
319-
----
320-
321-
Kotlin::
322-
+
323-
[source,kotlin,indent=0,subs="verbatim,quotes"]
324-
----
325-
@Configuration
326-
@EnableWebMvc
327-
class WebConfig : WebMvcConfigurer {
328-
329-
override fun addCorsMappings(registry: CorsRegistry) {
330-
331-
registry.addMapping("/api/**")
332-
.allowedOrigins("https://domain2.com")
333-
.allowedMethods("PUT", "DELETE")
334-
.allowedHeaders("header1", "header2", "header3")
335-
.exposedHeaders("header1", "header2")
336-
.allowCredentials(true).maxAge(3600)
337-
338-
// Add more mappings...
339-
}
340-
}
341-
----
342-
======
343-
344-
[[mvc-cors-global-xml]]
345-
=== XML Configuration
346-
347-
To enable CORS in the XML namespace, you can use the `<mvc:cors>` element,
348-
as the following example shows:
349-
350-
[source,xml,indent=0,subs="verbatim"]
351-
----
352-
<mvc:cors>
353-
354-
<mvc:mapping path="/api/**"
355-
allowed-origins="https://domain1.com, https://domain2.com"
356-
allowed-methods="GET, PUT"
357-
allowed-headers="header1, header2, header3"
358-
exposed-headers="header1, header2" allow-credentials="true"
359-
max-age="123" />
360-
361-
<mvc:mapping path="/resources/**"
362-
allowed-origins="https://domain1.com" />
363-
364-
</mvc:cors>
365-
----
289+
You can enable CORS in the Spring MVC configuration as the following example shows:
366290

291+
include-code::./WebConfiguration[tag=snippet,indent=0]
367292

368293
[[mvc-cors-filter]]
369294
== CORS Filter

framework-docs/modules/ROOT/pages/web/webmvc-functional.adoc

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -900,81 +900,9 @@ processing lifecycle and also (potentially) run side by side with annotated cont
900900
any are declared. It is also how functional endpoints are enabled by the Spring Boot Web
901901
starter.
902902

903-
The following example shows a WebMvc Java configuration:
903+
The following example shows a related Spring MVC configuration:
904904

905-
[tabs]
906-
======
907-
Java::
908-
+
909-
[source,java,indent=0,subs="verbatim,quotes"]
910-
----
911-
@Configuration
912-
@EnableMvc
913-
public class WebConfig implements WebMvcConfigurer {
914-
915-
@Bean
916-
public RouterFunction<?> routerFunctionA() {
917-
// ...
918-
}
919-
920-
@Bean
921-
public RouterFunction<?> routerFunctionB() {
922-
// ...
923-
}
924-
925-
// ...
926-
927-
@Override
928-
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
929-
// configure message conversion...
930-
}
931-
932-
@Override
933-
public void addCorsMappings(CorsRegistry registry) {
934-
// configure CORS...
935-
}
936-
937-
@Override
938-
public void configureViewResolvers(ViewResolverRegistry registry) {
939-
// configure view resolution for HTML rendering...
940-
}
941-
}
942-
----
943-
944-
Kotlin::
945-
+
946-
[source,kotlin,indent=0,subs="verbatim,quotes"]
947-
----
948-
@Configuration
949-
@EnableMvc
950-
class WebConfig : WebMvcConfigurer {
951-
952-
@Bean
953-
fun routerFunctionA(): RouterFunction<*> {
954-
// ...
955-
}
956-
957-
@Bean
958-
fun routerFunctionB(): RouterFunction<*> {
959-
// ...
960-
}
961-
962-
// ...
963-
964-
override fun configureMessageConverters(converters: List<HttpMessageConverter<*>>) {
965-
// configure message conversion...
966-
}
967-
968-
override fun addCorsMappings(registry: CorsRegistry) {
969-
// configure CORS...
970-
}
971-
972-
override fun configureViewResolvers(registry: ViewResolverRegistry) {
973-
// configure view resolution for HTML rendering...
974-
}
975-
}
976-
----
977-
======
905+
include-code::./WebConfiguration[tag=snippet,indent=0]
978906

979907

980908
[[webmvc-fn-handler-filter-function]]

framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-requestmapping.adoc

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -550,53 +550,7 @@ You can programmatically register handler methods, which you can use for dynamic
550550
registrations or for advanced cases, such as different instances of the same handler
551551
under different URLs. The following example registers a handler method:
552552

553-
[tabs]
554-
======
555-
Java::
556-
+
557-
[source,java,indent=0,subs="verbatim,quotes"]
558-
----
559-
@Configuration
560-
public class MyConfig {
561-
562-
@Autowired
563-
public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler) // <1>
564-
throws NoSuchMethodException {
565-
566-
RequestMappingInfo info = RequestMappingInfo
567-
.paths("/user/{id}").methods(RequestMethod.GET).build(); // <2>
568-
569-
Method method = UserHandler.class.getMethod("getUser", Long.class); // <3>
570-
571-
mapping.registerMapping(info, handler, method); // <4>
572-
}
573-
}
574-
----
575-
<1> Inject the target handler and the handler mapping for controllers.
576-
<2> Prepare the request mapping meta data.
577-
<3> Get the handler method.
578-
<4> Add the registration.
579-
580-
Kotlin::
581-
+
582-
[source,kotlin,indent=0,subs="verbatim,quotes"]
583-
----
584-
@Configuration
585-
class MyConfig {
586-
587-
@Autowired
588-
fun setHandlerMapping(mapping: RequestMappingHandlerMapping, handler: UserHandler) { // <1>
589-
val info = RequestMappingInfo.paths("/user/{id}").methods(RequestMethod.GET).build() // <2>
590-
val method = UserHandler::class.java.getMethod("getUser", Long::class.java) // <3>
591-
mapping.registerMapping(info, handler, method) // <4>
592-
}
593-
}
594-
----
595-
<1> Inject the target handler and the handler mapping for controllers.
596-
<2> Prepare the request mapping meta data.
597-
<3> Get the handler method.
598-
<4> Add the registration.
599-
======
553+
include-code::./MyConfiguration[tag=snippet,indent=0]
600554

601555

602556

framework-docs/modules/ROOT/pages/web/websocket/fallback.adoc

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,9 @@ from the iframe. By default, the iframe is set to download the SockJS client
152152
from a CDN location. It is a good idea to configure this option to use
153153
a URL from the same origin as the application.
154154

155-
The following example shows how to do so in Java configuration:
155+
The following example shows how to configure it:
156156

157-
[source,java,indent=0,subs="verbatim,quotes"]
158-
----
159-
@Configuration
160-
@EnableWebSocketMessageBroker
161-
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
162-
163-
@Override
164-
public void registerStompEndpoints(StompEndpointRegistry registry) {
165-
registry.addEndpoint("/portfolio").withSockJS()
166-
.setClientLibraryUrl("http://localhost:8080/myapp/js/sockjs-client.js");
167-
}
168-
169-
// ...
170-
171-
}
172-
----
173-
174-
The XML namespace provides a similar option through the `<websocket:sockjs>` element.
157+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
175158

176159
NOTE: During initial development, do enable the SockJS client `devel` mode that prevents
177160
the browser from caching SockJS requests (like the iframe) that would otherwise
@@ -307,23 +290,4 @@ jettyHttpClient.setExecutor(new QueuedThreadPool(1000));
307290
The following example shows the server-side SockJS-related properties (see javadoc for details)
308291
that you should also consider customizing:
309292

310-
[source,java,indent=0,subs="verbatim,quotes"]
311-
----
312-
@Configuration
313-
public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport {
314-
315-
@Override
316-
public void registerStompEndpoints(StompEndpointRegistry registry) {
317-
registry.addEndpoint("/sockjs").withSockJS()
318-
.setStreamBytesLimit(512 * 1024) <1>
319-
.setHttpMessageCacheSize(1000) <2>
320-
.setDisconnectDelay(30 * 1000); <3>
321-
}
322-
323-
// ...
324-
}
325-
----
326-
<1> Set the `streamBytesLimit` property to 512KB (the default is 128KB -- `128 * 1024`).
327-
<2> Set the `httpMessageCacheSize` property to 1,000 (the default is `100`).
328-
<3> Set the `disconnectDelay` property to 30 property seconds (the default is five seconds
329-
-- `5 * 1000`).
293+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-present 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.docs.web.mvccorsglobal;
18+
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
21+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
22+
23+
// tag::snippet[]
24+
@Configuration
25+
public class WebConfiguration implements WebMvcConfigurer {
26+
27+
@Override
28+
public void addCorsMappings(CorsRegistry registry) {
29+
registry.addMapping("/api/**")
30+
.allowedOrigins("https://domain1.com", "https://domain2.com")
31+
.allowedMethods("GET", "PUT")
32+
.allowedHeaders("header1", "header2", "header3")
33+
.exposedHeaders("header1", "header2")
34+
.allowCredentials(true)
35+
.maxAge(3600);
36+
37+
// Add more mappings...
38+
}
39+
}
40+
// end::snippet[]
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2002-present 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.docs.web.webmvc.mvccontroller.mvcannrequestmappingregistration;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.web.bind.annotation.RequestMethod;
24+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
25+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
26+
27+
// tag::snippet[]
28+
@Configuration
29+
public class MyConfiguration {
30+
31+
// Inject the target handler and the handler mapping for controllers
32+
@Autowired
33+
public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler)
34+
throws NoSuchMethodException {
35+
36+
// Prepare the request mapping meta data
37+
RequestMappingInfo info = RequestMappingInfo
38+
.paths("/user/{id}").methods(RequestMethod.GET).build();
39+
40+
// Get the handler method
41+
Method method = UserHandler.class.getMethod("getUser", Long.class);
42+
43+
// Add the registration
44+
mapping.registerMapping(info, handler, method);
45+
}
46+
}
47+
// end::snippet[]
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2002-present 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.docs.web.webmvc.mvccontroller.mvcannrequestmappingregistration;
18+
19+
public class UserHandler {
20+
21+
public void getUser(Long id) {
22+
// ...
23+
}
24+
}
25+

0 commit comments

Comments
 (0)