Use JsonMapper instead of ObjectMapper when relevant

This commit updates Jackson 3 support to use JsonMapper instead
of ObjectMapper in converter, codec and view constructors.

Closes gh-35282
This commit is contained in:
Sébastien Deleuze
2025-08-12 10:36:37 +02:00
parent bde806b7fc
commit d115f36400
17 changed files with 94 additions and 105 deletions
@@ -32,7 +32,6 @@ import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import org.jspecify.annotations.Nullable;
import tools.jackson.databind.JavaType;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.ObjectWriter;
import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.json.JsonMapper;
@@ -63,7 +62,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
public static final String DEFAULT_ENCODING = "UTF-8";
private final ObjectMapper objectMapper;
private final JsonMapper jsonMapper;
private MessageType targetType = MessageType.BYTES;
@@ -86,17 +85,17 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
* {@link MapperBuilder#findModules(ClassLoader)}.
*/
public JacksonJsonMessageConverter() {
this.objectMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build();
this.jsonMapper = JsonMapper.builder().findAndAddModules(JacksonJsonMessageConverter.class.getClassLoader()).build();
}
/**
* Construct a new instance with the provided {@link ObjectMapper}.
* Construct a new instance with the provided {@link JsonMapper}.
* @see JsonMapper#builder()
* @see MapperBuilder#findModules(ClassLoader)
*/
public JacksonJsonMessageConverter(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
public JacksonJsonMessageConverter(JsonMapper jsonMapper) {
Assert.notNull(jsonMapper, "JsonMapper must not be null");
this.jsonMapper = jsonMapper;
}
/**
@@ -173,9 +172,9 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
Message message;
try {
message = switch (this.targetType) {
case TEXT -> mapToTextMessage(object, session, this.objectMapper.writer());
case BYTES -> mapToBytesMessage(object, session, this.objectMapper.writer());
default -> mapToMessage(object, session, this.objectMapper.writer(), this.targetType);
case TEXT -> mapToTextMessage(object, session, this.jsonMapper.writer());
case BYTES -> mapToBytesMessage(object, session, this.jsonMapper.writer());
default -> mapToMessage(object, session, this.jsonMapper.writer(), this.targetType);
};
}
catch (IOException ex) {
@@ -206,10 +205,10 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
throws JMSException, MessageConversionException {
if (jsonView != null) {
return toMessage(object, session, this.objectMapper.writerWithView(jsonView));
return toMessage(object, session, this.jsonMapper.writerWithView(jsonView));
}
else {
return toMessage(object, session, this.objectMapper.writer());
return toMessage(object, session, this.jsonMapper.writer());
}
}
@@ -363,7 +362,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
throws JMSException, IOException {
String body = message.getText();
return this.objectMapper.readValue(body, targetJavaType);
return this.jsonMapper.readValue(body, targetJavaType);
}
/**
@@ -386,7 +385,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
if (encoding != null) {
try {
String body = new String(bytes, encoding);
return this.objectMapper.readValue(body, targetJavaType);
return this.jsonMapper.readValue(body, targetJavaType);
}
catch (UnsupportedEncodingException ex) {
throw new MessageConversionException("Cannot convert bytes to String", ex);
@@ -394,7 +393,7 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
}
else {
// Jackson internally performs encoding detection, falling back to UTF-8.
return this.objectMapper.readValue(bytes, targetJavaType);
return this.jsonMapper.readValue(bytes, targetJavaType);
}
}
@@ -437,11 +436,11 @@ public class JacksonJsonMessageConverter implements SmartMessageConverter, BeanC
}
Class<?> mappedClass = this.idClassMappings.get(typeId);
if (mappedClass != null) {
return this.objectMapper.constructType(mappedClass);
return this.jsonMapper.constructType(mappedClass);
}
try {
Class<?> typeClass = ClassUtils.forName(typeId, this.beanClassLoader);
return this.objectMapper.constructType(typeClass);
return this.jsonMapper.constructType(typeClass);
}
catch (Throwable ex) {
throw new MessageConversionException("Failed to resolve type id [" + typeId + "]", ex);