Skip to content

Commit fc2e51a

Browse files
committed
Preserve ordering when auto-configuring WebSocket MessageConverters
Previously, WebSocketMessagingAutoConfiguration added a single additional converter. This was a MappingJackson2MessageConverter configured with the auto-configured ObjectMapper. AbstractMessageBrokerConfiguration places additional converters before any of the default converters. This meant that the auto-configuration had the unwanted side-effect of changing the ordering of the converters. A MappingJackson2MessageConverter was now first in the list, whereas, by default, it's last in the list after a StringMessageConverter and a ByteArrayMessageConverter. This commit updates WebSocketMessagingAutoConfiguration so that it switches off the registration of the default converters and registers a StringMessageConverter, ByteArrayMessageConverter and MappingJackson2MessageConverter in that order. A test has been added to verify that the types of these three converters match the types of the default converters. A second test that verifies that String responses are converted correctly has also been added alongside the existing test that verified the behaviour for JSON responses. Closes gh-5123
1 parent c10943c commit fc2e51a

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/websocket/WebSocketMessagingAutoConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2929
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
3030
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.messaging.converter.ByteArrayMessageConverter;
3132
import org.springframework.messaging.converter.DefaultContentTypeResolver;
3233
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
3334
import org.springframework.messaging.converter.MessageConverter;
35+
import org.springframework.messaging.converter.StringMessageConverter;
3436
import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration;
3537
import org.springframework.util.MimeTypeUtils;
3638
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
@@ -72,8 +74,10 @@ public boolean configureMessageConverters(
7274
DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
7375
resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
7476
converter.setContentTypeResolver(resolver);
77+
messageConverters.add(new StringMessageConverter());
78+
messageConverters.add(new ByteArrayMessageConverter());
7579
messageConverters.add(converter);
76-
return true;
80+
return false;
7781
}
7882

7983
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/websocket/WebSocketMessagingAutoConfigurationTests.java

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package org.springframework.boot.autoconfigure.websocket;
1818

1919
import java.lang.reflect.Type;
20+
import java.util.ArrayList;
2021
import java.util.Arrays;
22+
import java.util.Iterator;
2123
import java.util.List;
2224
import java.util.concurrent.CountDownLatch;
2325
import java.util.concurrent.TimeUnit;
2426
import java.util.concurrent.atomic.AtomicReference;
2527

28+
import com.fasterxml.jackson.databind.ObjectMapper;
2629
import org.junit.After;
2730
import org.junit.Before;
2831
import org.junit.Test;
@@ -39,6 +42,8 @@
3942
import org.springframework.boot.test.EnvironmentTestUtils;
4043
import org.springframework.context.annotation.Bean;
4144
import org.springframework.context.annotation.Configuration;
45+
import org.springframework.messaging.converter.CompositeMessageConverter;
46+
import org.springframework.messaging.converter.MessageConverter;
4247
import org.springframework.messaging.converter.SimpleMessageConverter;
4348
import org.springframework.messaging.simp.annotation.SubscribeMapping;
4449
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
@@ -49,9 +54,11 @@
4954
import org.springframework.messaging.simp.stomp.StompSessionHandler;
5055
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
5156
import org.springframework.stereotype.Controller;
57+
import org.springframework.test.util.ReflectionTestUtils;
5258
import org.springframework.web.client.RestTemplate;
5359
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
5460
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
61+
import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration;
5562
import org.springframework.web.socket.config.annotation.EnableWebSocket;
5663
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
5764
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@@ -62,6 +69,7 @@
6269
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
6370

6471
import static org.hamcrest.Matchers.equalTo;
72+
import static org.hamcrest.Matchers.instanceOf;
6573
import static org.hamcrest.Matchers.is;
6674
import static org.junit.Assert.assertThat;
6775
import static org.junit.Assert.fail;
@@ -92,7 +100,49 @@ public void tearDown() {
92100
}
93101

94102
@Test
95-
public void basicMessagingWithJson() throws Throwable {
103+
public void basicMessagingWithJsonResponse() throws Throwable {
104+
Object result = performStompSubscription("/app/json");
105+
assertThat(new String((byte[]) result),
106+
is(equalTo(String.format("{%n \"foo\" : 5,%n \"bar\" : \"baz\"%n}"))));
107+
}
108+
109+
@Test
110+
public void basicMessagingWithStringResponse() throws Throwable {
111+
Object result = performStompSubscription("/app/string");
112+
assertThat(new String((byte[]) result),
113+
is(equalTo(String.format("string data"))));
114+
}
115+
116+
@Test
117+
public void customizedConverterTypesMatchDefaultConverterTypes() {
118+
List<MessageConverter> customizedConverters = getCustomizedConverters();
119+
List<MessageConverter> defaultConverters = getDefaultConverters();
120+
assertThat(customizedConverters.size(), is(equalTo(defaultConverters.size())));
121+
Iterator<MessageConverter> customizedIterator = customizedConverters.iterator();
122+
Iterator<MessageConverter> defaultIterator = defaultConverters.iterator();
123+
while (customizedIterator.hasNext()) {
124+
assertThat(customizedIterator.next(),
125+
is(instanceOf(defaultIterator.next().getClass())));
126+
}
127+
}
128+
129+
private List<MessageConverter> getCustomizedConverters() {
130+
List<MessageConverter> customizedConverters = new ArrayList<MessageConverter>();
131+
WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration configuration = new WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration();
132+
ReflectionTestUtils.setField(configuration, "objectMapper", new ObjectMapper());
133+
configuration.configureMessageConverters(customizedConverters);
134+
return customizedConverters;
135+
}
136+
137+
@SuppressWarnings("unchecked")
138+
private List<MessageConverter> getDefaultConverters() {
139+
CompositeMessageConverter compositeDefaultConverter = new DelegatingWebSocketMessageBrokerConfiguration()
140+
.brokerMessageConverter();
141+
return (List<MessageConverter>) ReflectionTestUtils
142+
.getField(compositeDefaultConverter, "converters");
143+
}
144+
145+
private Object performStompSubscription(final String topic) throws Throwable {
96146
EnvironmentTestUtils.addEnvironment(this.context, "server.port:0",
97147
"spring.jackson.serialization.indent-output:true");
98148
this.context.register(WebSocketMessagingConfiguration.class);
@@ -107,7 +157,7 @@ public void basicMessagingWithJson() throws Throwable {
107157
@Override
108158
public void afterConnected(StompSession session,
109159
StompHeaders connectedHeaders) {
110-
session.subscribe("/app/data", new StompFrameHandler() {
160+
session.subscribe(topic, new StompFrameHandler() {
111161

112162
@Override
113163
public void handleFrame(StompHeaders headers, Object payload) {
@@ -155,8 +205,8 @@ public void handleTransportError(StompSession session, Throwable exception) {
155205
fail("Response was not received within 30 seconds");
156206
}
157207
}
158-
assertThat(new String((byte[]) result.get()),
159-
is(equalTo(String.format("{%n \"foo\" : 5,%n \"bar\" : \"baz\"%n}"))));
208+
209+
return result.get();
160210
}
161211

162212
@Configuration
@@ -201,11 +251,16 @@ public TomcatWebSocketContainerCustomizer tomcatCustomizer() {
201251
@Controller
202252
static class MessagingController {
203253

204-
@SubscribeMapping("/data")
205-
Data getData() {
254+
@SubscribeMapping("/json")
255+
Data json() {
206256
return new Data(5, "baz");
207257
}
208258

259+
@SubscribeMapping("/string")
260+
String string() {
261+
return "string data";
262+
}
263+
209264
}
210265

211266
static class Data {

0 commit comments

Comments
 (0)