Skip to content

Commit 246f291

Browse files
committed
Use code includes and tabs in WebSocket documentation
See gh-22171
1 parent 4eb93da commit 246f291

File tree

74 files changed

+2139
-668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+2139
-668
lines changed

framework-docs/framework-docs.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ dependencies {
6666
api(project(":spring-webmvc"))
6767
api(project(":spring-context-support"))
6868
api(project(":spring-aspects"))
69+
api(project(":spring-websocket"))
6970

7071
api("org.jetbrains.kotlin:kotlin-stdlib")
7172
api("jakarta.jms:jakarta.jms-api")
@@ -76,6 +77,7 @@ dependencies {
7677
api("com.fasterxml.jackson.module:jackson-module-parameter-names")
7778
api("jakarta.validation:jakarta.validation-api")
7879
api("org.aspectj:aspectjweaver")
80+
api("io.projectreactor.netty:reactor-netty-http")
7981

8082
implementation(project(":spring-core-test"))
8183
implementation("org.assertj:assertj-core")

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -82,49 +82,9 @@ https://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html[narrated tes
8282
[[websocket-fallback-sockjs-enable]]
8383
== Enabling SockJS
8484

85-
You can enable SockJS through Java configuration, as the following example shows:
85+
You can enable SockJS through configuration, as the following example shows:
8686

87-
[source,java,indent=0,subs="verbatim,quotes"]
88-
----
89-
@Configuration
90-
@EnableWebSocket
91-
public class WebSocketConfig implements WebSocketConfigurer {
92-
93-
@Override
94-
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
95-
registry.addHandler(myHandler(), "/myHandler").withSockJS();
96-
}
97-
98-
@Bean
99-
public WebSocketHandler myHandler() {
100-
return new MyHandler();
101-
}
102-
103-
}
104-
----
105-
106-
The following example shows the XML configuration equivalent of the preceding example:
107-
108-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
109-
----
110-
<beans xmlns="http://www.springframework.org/schema/beans"
111-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
112-
xmlns:websocket="http://www.springframework.org/schema/websocket"
113-
xsi:schemaLocation="
114-
http://www.springframework.org/schema/beans
115-
https://www.springframework.org/schema/beans/spring-beans.xsd
116-
http://www.springframework.org/schema/websocket
117-
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
118-
119-
<websocket:handlers>
120-
<websocket:mapping path="/myHandler" handler="myHandler"/>
121-
<websocket:sockjs/>
122-
</websocket:handlers>
123-
124-
<bean id="myHandler" class="org.springframework.samples.MyHandler"/>
125-
126-
</beans>
127-
----
87+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
12888

12989
The preceding example is for use in Spring MVC applications and should be included in the
13090
configuration of a xref:web/webmvc/mvc-servlet.adoc[`DispatcherServlet`]. However, Spring's WebSocket

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

Lines changed: 10 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,12 @@ Creating a WebSocket server is as simple as implementing `WebSocketHandler` or,
1616
likely, extending either `TextWebSocketHandler` or `BinaryWebSocketHandler`. The following
1717
example uses `TextWebSocketHandler`:
1818

19-
[source,java,indent=0,subs="verbatim,quotes"]
20-
----
21-
import org.springframework.web.socket.WebSocketHandler;
22-
import org.springframework.web.socket.WebSocketSession;
23-
import org.springframework.web.socket.TextMessage;
24-
25-
public class MyHandler extends TextWebSocketHandler {
26-
27-
@Override
28-
public void handleTextMessage(WebSocketSession session, TextMessage message) {
29-
// ...
30-
}
31-
32-
}
33-
----
19+
include-code::./MyHandler[tag=snippet,indent=0]
3420

35-
There is dedicated WebSocket Java configuration and XML namespace support for mapping the preceding
21+
There is dedicated WebSocket programmatic configuration and XML namespace support for mapping the preceding
3622
WebSocket handler to a specific URL, as the following example shows:
3723

38-
[source,java,indent=0,subs="verbatim,quotes"]
39-
----
40-
import org.springframework.web.socket.config.annotation.EnableWebSocket;
41-
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
42-
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
43-
44-
@Configuration
45-
@EnableWebSocket
46-
public class WebSocketConfig implements WebSocketConfigurer {
47-
48-
@Override
49-
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
50-
registry.addHandler(myHandler(), "/myHandler");
51-
}
52-
53-
@Bean
54-
public WebSocketHandler myHandler() {
55-
return new MyHandler();
56-
}
57-
58-
}
59-
----
60-
61-
The following example shows the XML configuration equivalent of the preceding example:
62-
63-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
64-
----
65-
<beans xmlns="http://www.springframework.org/schema/beans"
66-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
67-
xmlns:websocket="http://www.springframework.org/schema/websocket"
68-
xsi:schemaLocation="
69-
http://www.springframework.org/schema/beans
70-
https://www.springframework.org/schema/beans/spring-beans.xsd
71-
http://www.springframework.org/schema/websocket
72-
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
73-
74-
<websocket:handlers>
75-
<websocket:mapping path="/myHandler" handler="myHandler"/>
76-
</websocket:handlers>
77-
78-
<bean id="myHandler" class="org.springframework.samples.MyHandler"/>
79-
80-
</beans>
81-
----
24+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
8225

8326
The preceding example is for use in Spring MVC applications and should be included
8427
in the configuration of a xref:web/webmvc/mvc-servlet.adoc[`DispatcherServlet`]. However, Spring's
@@ -104,45 +47,7 @@ You can use such an interceptor to preclude the handshake or to make any attribu
10447
available to the `WebSocketSession`. The following example uses a built-in interceptor
10548
to pass HTTP session attributes to the WebSocket session:
10649

107-
[source,java,indent=0,subs="verbatim,quotes"]
108-
----
109-
@Configuration
110-
@EnableWebSocket
111-
public class WebSocketConfig implements WebSocketConfigurer {
112-
113-
@Override
114-
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
115-
registry.addHandler(new MyHandler(), "/myHandler")
116-
.addInterceptors(new HttpSessionHandshakeInterceptor());
117-
}
118-
119-
}
120-
----
121-
122-
The following example shows the XML configuration equivalent of the preceding example:
123-
124-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
125-
----
126-
<beans xmlns="http://www.springframework.org/schema/beans"
127-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
128-
xmlns:websocket="http://www.springframework.org/schema/websocket"
129-
xsi:schemaLocation="
130-
http://www.springframework.org/schema/beans
131-
https://www.springframework.org/schema/beans/spring-beans.xsd
132-
http://www.springframework.org/schema/websocket
133-
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
134-
135-
<websocket:handlers>
136-
<websocket:mapping path="/myHandler" handler="myHandler"/>
137-
<websocket:handshake-interceptors>
138-
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
139-
</websocket:handshake-interceptors>
140-
</websocket:handlers>
141-
142-
<bean id="myHandler" class="org.springframework.samples.MyHandler"/>
143-
144-
</beans>
145-
----
50+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
14651

14752
A more advanced option is to extend the `DefaultHandshakeHandler` that performs
14853
the steps of the WebSocket handshake, including validating the client origin,
@@ -236,69 +141,17 @@ You can configure of the underlying WebSocket server such as input message buffe
236141
idle timeout, and more.
237142

238143
For Jakarta WebSocket servers, you can add a `ServletServerContainerFactoryBean` to your
239-
Java configuration. For example:
240-
241-
[source,java,indent=0,subs="verbatim,quotes"]
242-
----
243-
@Bean
244-
public ServletServerContainerFactoryBean createWebSocketContainer() {
245-
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
246-
container.setMaxTextMessageBufferSize(8192);
247-
container.setMaxBinaryMessageBufferSize(8192);
248-
return container;
249-
}
250-
----
251-
252-
Or to your XML configuration:
144+
configuration. For example:
253145

254-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
255-
----
256-
<beans xmlns="http://www.springframework.org/schema/beans"
257-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
258-
xmlns:websocket="http://www.springframework.org/schema/websocket"
259-
xsi:schemaLocation="
260-
http://www.springframework.org/schema/beans
261-
https://www.springframework.org/schema/beans/spring-beans.xsd
262-
http://www.springframework.org/schema/websocket
263-
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
264-
265-
<bean class="org.springframework...ServletServerContainerFactoryBean">
266-
<property name="maxTextMessageBufferSize" value="8192"/>
267-
<property name="maxBinaryMessageBufferSize" value="8192"/>
268-
</bean>
269-
270-
</beans>
271-
----
146+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
272147

273148
NOTE: For client Jakarta WebSocket configuration, use
274-
ContainerProvider.getWebSocketContainer() in Java configuration, or
149+
ContainerProvider.getWebSocketContainer() in programmatic configuration, or
275150
`WebSocketContainerFactoryBean` in XML.
276151

277-
For Jetty, you can supply a `Consumer` callback to configure the WebSocket server:
152+
For Jetty, you can supply a callback to configure the WebSocket server:
278153

279-
[source,java,indent=0,subs="verbatim,quotes"]
280-
----
281-
@Configuration
282-
@EnableWebSocket
283-
public class WebSocketConfig implements WebSocketConfigurer {
284-
285-
@Override
286-
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
287-
registry.addHandler(echoWebSocketHandler(), "/echo").setHandshakeHandler(handshakeHandler());
288-
}
289-
290-
@Bean
291-
public DefaultHandshakeHandler handshakeHandler() {
292-
JettyRequestUpgradeStrategy strategy = new JettyRequestUpgradeStrategy();
293-
strategy.addWebSocketConfigurer(configurable -> {
294-
policy.setInputBufferSize(8192);
295-
policy.setIdleTimeout(600000);
296-
});
297-
return new DefaultHandshakeHandler(strategy);
298-
}
299-
300-
}
301-
----
154+
include-code::./JettyWebSocketConfiguration[tag=snippet,indent=0]
302155

303156
TIP: When using STOMP over WebSocket, you will also need to configure
304157
xref:web/websocket/stomp/server-config.adoc[STOMP WebSocket transport]
@@ -331,51 +184,4 @@ The three possible behaviors are:
331184

332185
You can configure WebSocket and SockJS allowed origins, as the following example shows:
333186

334-
[source,java,indent=0,subs="verbatim,quotes"]
335-
----
336-
import org.springframework.web.socket.config.annotation.EnableWebSocket;
337-
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
338-
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
339-
340-
@Configuration
341-
@EnableWebSocket
342-
public class WebSocketConfig implements WebSocketConfigurer {
343-
344-
@Override
345-
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
346-
registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("https://mydomain.com");
347-
}
348-
349-
@Bean
350-
public WebSocketHandler myHandler() {
351-
return new MyHandler();
352-
}
353-
354-
}
355-
----
356-
357-
The following example shows the XML configuration equivalent of the preceding example:
358-
359-
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
360-
----
361-
<beans xmlns="http://www.springframework.org/schema/beans"
362-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
363-
xmlns:websocket="http://www.springframework.org/schema/websocket"
364-
xsi:schemaLocation="
365-
http://www.springframework.org/schema/beans
366-
https://www.springframework.org/schema/beans/spring-beans.xsd
367-
http://www.springframework.org/schema/websocket
368-
https://www.springframework.org/schema/websocket/spring-websocket.xsd">
369-
370-
<websocket:handlers allowed-origins="https://mydomain.com">
371-
<websocket:mapping path="/myHandler" handler="myHandler" />
372-
</websocket:handlers>
373-
374-
<bean id="myHandler" class="org.springframework.samples.MyHandler"/>
375-
376-
</beans>
377-
----
378-
379-
380-
381-
187+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]

framework-docs/modules/ROOT/pages/web/websocket/stomp/authentication-token-based.adoc

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,7 @@ the user header on the CONNECT `Message`. Spring notes and saves the authenticat
4040
user and associate it with subsequent STOMP messages on the same session. The following
4141
example shows how to register a custom authentication interceptor:
4242

43-
[source,java,indent=0,subs="verbatim,quotes"]
44-
----
45-
@Configuration
46-
@EnableWebSocketMessageBroker
47-
public class MyConfig implements WebSocketMessageBrokerConfigurer {
48-
49-
@Override
50-
public void configureClientInboundChannel(ChannelRegistration registration) {
51-
registration.interceptors(new ChannelInterceptor() {
52-
@Override
53-
public Message<?> preSend(Message<?> message, MessageChannel channel) {
54-
StompHeaderAccessor accessor =
55-
MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
56-
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
57-
Authentication user = ... ; // access authentication header(s)
58-
accessor.setUser(user);
59-
}
60-
return message;
61-
}
62-
});
63-
}
64-
}
65-
----
43+
include-code::./WebSocketConfiguration[tag=snippet,indent=0]
6644

6745
Also, note that, when you use Spring Security's authorization for messages, at present,
6846
you need to ensure that the authentication `ChannelInterceptor` config is ordered

0 commit comments

Comments
 (0)