Introduce Jackson 3 support for spring-websocket

This commit introduces a JacksonJsonSockJsMessageCodec Jackson 3 variant
of Jackson2SockJsMessageCodec.

See gh-33798
This commit is contained in:
Sébastien Deleuze
2025-05-13 12:58:20 +02:00
parent a0ed3f052e
commit ac3c1b8762
12 changed files with 110 additions and 31 deletions

View File

@@ -23,8 +23,6 @@ import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.SimpSessionScope;
import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler;
@@ -162,17 +160,4 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac
return stats;
}
@Override
protected MappingJackson2MessageConverter createJacksonConverter() {
MappingJackson2MessageConverter messageConverter = super.createJacksonConverter();
// Use Jackson builder in order to have well-known modules registered automatically.
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
ApplicationContext applicationContext = getApplicationContext();
if (applicationContext != null) {
builder.applicationContext(applicationContext);
}
messageConverter.setObjectMapper(builder.build());
return messageConverter;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -47,6 +47,7 @@ import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 4.1
*/
public abstract class AbstractClientSockJsSession implements WebSocketSession {
@@ -268,7 +269,7 @@ public abstract class AbstractClientSockJsSession implements WebSocketSession {
try {
messages = getMessageCodec().decode(frameData);
}
catch (IOException ex) {
catch (RuntimeException | IOException ex) {
if (logger.isErrorEnabled()) {
logger.error("Failed to decode data for SockJS \"message\" frame: " + frame + " in " + this, ex);
}

View File

@@ -40,6 +40,7 @@ import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.WebSocketClient;
import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec;
import org.springframework.web.socket.sockjs.frame.JacksonJsonSockJsMessageCodec;
import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
import org.springframework.web.socket.sockjs.transport.TransportType;
import org.springframework.web.util.UriComponentsBuilder;
@@ -62,6 +63,9 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public class SockJsClient implements WebSocketClient, Lifecycle {
private static final boolean jacksonPresent = ClassUtils.isPresent(
"tools.jackson.databind.ObjectMapper", SockJsClient.class.getClassLoader());
private static final boolean jackson2Present = ClassUtils.isPresent(
"com.fasterxml.jackson.databind.ObjectMapper", SockJsClient.class.getClassLoader());
@@ -97,7 +101,10 @@ public class SockJsClient implements WebSocketClient, Lifecycle {
Assert.notEmpty(transports, "No transports provided");
this.transports = new ArrayList<>(transports);
this.infoReceiver = initInfoReceiver(transports);
if (jackson2Present) {
if (jacksonPresent) {
this.messageCodec = new JacksonJsonSockJsMessageCodec();
}
else if (jackson2Present) {
this.messageCodec = new Jackson2SockJsMessageCodec();
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2025 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.web.socket.sockjs.frame;
import java.io.InputStream;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.jspecify.annotations.Nullable;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper;
import org.springframework.util.Assert;
/**
* A Jackson 3.x codec for encoding and decoding SockJS messages.
*
* <p>The default constructor loads {@link tools.jackson.databind.JacksonModule}s
* found by {@link MapperBuilder#findModules(ClassLoader)}.
*
* @author Sebastien Deleuze
* @since 7.0
*/
public class JacksonJsonSockJsMessageCodec extends AbstractSockJsMessageCodec {
private final ObjectMapper objectMapper;
/**
* Construct a new instance with a {@link JsonMapper} customized with the
* {@link tools.jackson.databind.JacksonModule}s found by
* {@link MapperBuilder#findModules(ClassLoader)}.
*/
public JacksonJsonSockJsMessageCodec() {
this.objectMapper = JsonMapper.builder().findAndAddModules(JacksonJsonSockJsMessageCodec.class.getClassLoader()).build();
}
/**
* Construct a new instance with the provided {@link ObjectMapper}.
* @see JsonMapper#builder()
* @see MapperBuilder#findAndAddModules(ClassLoader)
*/
public JacksonJsonSockJsMessageCodec(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
}
@Override
public String @Nullable [] decode(String content) {
return this.objectMapper.readValue(content, String[].class);
}
@Override
public String @Nullable [] decodeInputStream(InputStream content) {
return this.objectMapper.readValue(content, String[].class);
}
@Override
protected char[] applyJsonQuoting(String content) {
return JsonStringEncoder.getInstance().quoteAsString(content);
}
}

View File

@@ -51,6 +51,7 @@ import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.support.HandshakeInterceptorChain;
import org.springframework.web.socket.sockjs.SockJsException;
import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec;
import org.springframework.web.socket.sockjs.frame.JacksonJsonSockJsMessageCodec;
import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec;
import org.springframework.web.socket.sockjs.support.AbstractSockJsService;
@@ -70,6 +71,9 @@ import org.springframework.web.socket.sockjs.support.AbstractSockJsService;
*/
public class TransportHandlingSockJsService extends AbstractSockJsService implements SockJsServiceConfig, Lifecycle {
private static final boolean jacksonPresent = ClassUtils.isPresent(
"tools.jackson.databind.ObjectMapper", TransportHandlingSockJsService.class.getClassLoader());
private static final boolean jackson2Present = ClassUtils.isPresent(
"com.fasterxml.jackson.databind.ObjectMapper", TransportHandlingSockJsService.class.getClassLoader());
@@ -118,7 +122,10 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
}
}
if (jackson2Present) {
if (jacksonPresent) {
this.messageCodec = new JacksonJsonSockJsMessageCodec();
}
else if (jackson2Present) {
this.messageCodec = new Jackson2SockJsMessageCodec();
}
}