Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ project('spring-integration-jdbc') {
dependencies {
api 'org.springframework:spring-jdbc'
optionalApi "org.postgresql:postgresql:$postgresVersion"
optionalApi 'tools.jackson.core:jackson-databind'

testImplementation "com.h2database:h2:$h2Version"
testImplementation "org.hsqldb:hsqldb:$hsqldbVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.springframework.integration.support.converter.AllowListDeserializingConverter;
import org.springframework.integration.util.UUIDConverter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jmx.export.annotation.ManagedAttribute;
Expand Down Expand Up @@ -89,6 +90,7 @@
* @author Trung Pham
* @author Johannes Edmeier
* @author Ngoc Nhan
* @author Yoobin Yoon
*
* @since 2.2
*/
Expand Down Expand Up @@ -148,7 +150,7 @@ private enum Query {
private SerializingConverter serializer;

@SuppressWarnings("NullAway.Init")
private MessageRowMapper messageRowMapper;
private RowMapper<Message<?>> messageRowMapper;

@SuppressWarnings("NullAway.Init")
private ChannelMessageStorePreparedStatementSetter preparedStatementSetter;
Expand Down Expand Up @@ -232,13 +234,13 @@ public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
}

/**
* Allow for passing in a custom {@link MessageRowMapper}. The {@link MessageRowMapper}
* is used to convert the selected database row representing the persisted
* message into the actual {@link Message} object.
* Allow for passing in a custom {@link RowMapper} for {@link Message}.
* The {@link RowMapper} is used to convert the selected database row
* representing the persisted message into the actual {@link Message} object.
* @param messageRowMapper Must not be null
*/
public void setMessageRowMapper(MessageRowMapper messageRowMapper) {
Assert.notNull(messageRowMapper, "The provided MessageRowMapper must not be null.");
public void setMessageRowMapper(RowMapper<Message<?>> messageRowMapper) {
Assert.notNull(messageRowMapper, "The provided RowMapper must not be null.");
this.messageRowMapper = messageRowMapper;
}

Expand Down Expand Up @@ -388,7 +390,7 @@ protected MessageGroupFactory getMessageGroupFactory() {
* Check mandatory properties ({@link DataSource} and
* {@link #setChannelMessageStoreQueryProvider(ChannelMessageStoreQueryProvider)}). If no {@link MessageRowMapper}
* and {@link ChannelMessageStorePreparedStatementSetter} was explicitly set using
* {@link #setMessageRowMapper(MessageRowMapper)} and
* {@link #setMessageRowMapper(RowMapper)} and
* {@link #setPreparedStatementSetter(ChannelMessageStorePreparedStatementSetter)} respectively, the default
* {@link MessageRowMapper} and {@link ChannelMessageStorePreparedStatementSetter} will be instantiated using the
* specified {@link #deserializer}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2025-present 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.integration.jdbc.store.channel;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import tools.jackson.core.JacksonException;
import tools.jackson.databind.ObjectMapper;

import org.springframework.integration.support.json.JacksonMessagingUtils;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;

/**
* A {@link ChannelMessageStorePreparedStatementSetter} implementation that uses Jackson
* to serialize {@link Message} objects to JSON format instead of Java serialization.
* <p>
* This implementation stores the entire message (including headers and payload) as JSON,
* with type information embedded using Jackson's {@code @class} property.
* <p>
* <b>IMPORTANT:</b> JSON serialization exposes message content in text format in the database.
* Ensure proper database access controls and encryption for sensitive data.
* Consider the security implications before using this in production with sensitive information.
* <p>
* The {@link ObjectMapper} is configured using {@link JacksonMessagingUtils#messagingAwareMapper(String...)}
* which includes custom serializers/deserializers for Spring Integration message types
* and embeds class type information for secure deserialization.
* <p>
* <b>Database Requirements:</b>
* This implementation requires modifying the MESSAGE_CONTENT column to a text-based type:
* <ul>
* <li>PostgreSQL: Change from {@code BYTEA} to {@code JSONB}</li>
* <li>MySQL: Change from {@code BLOB} to {@code JSON}</li>
* <li>H2: Change from {@code LONGVARBINARY} to {@code CLOB}</li>
* </ul>
* See the reference documentation for schema migration instructions.
* <p>
* <b>Usage Example:</b>
* <pre>{@code
* &#64;Bean
* JdbcChannelMessageStore messageStore(DataSource dataSource) {
* JdbcChannelMessageStore store = new JdbcChannelMessageStore(dataSource);
* store.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider());
*
* // Enable JSON serialization (requires schema modification)
* store.setPreparedStatementSetter(
* new JacksonChannelMessageStorePreparedStatementSetter());
* store.setMessageRowMapper(
* new JacksonMessageRowMapper("com.example"));
*
* return store;
* }
* }</pre>
*
* @author Yoobin Yoon
*
* @since 7.0
*/
public class JacksonChannelMessageStorePreparedStatementSetter extends ChannelMessageStorePreparedStatementSetter {

private final ObjectMapper objectMapper;

/**
* Create a new {@link JacksonChannelMessageStorePreparedStatementSetter} with the
* default trusted packages from {@link JacksonMessagingUtils#DEFAULT_TRUSTED_PACKAGES}.
* <p>
* This constructor is suitable when you only need to serialize standard Spring Integration
* and Java classes. Custom payload types will require their package to be added to the
* corresponding {@link JacksonMessageRowMapper}.
*/
public JacksonChannelMessageStorePreparedStatementSetter() {
super();
this.objectMapper = JacksonMessagingUtils.messagingAwareMapper();
}

/**
* Create a new {@link JacksonChannelMessageStorePreparedStatementSetter} with a
* custom {@link ObjectMapper}.
* <p>
* This constructor allows full control over the JSON serialization configuration.
* The provided mapper should be configured appropriately for Message serialization,
* typically using {@link JacksonMessagingUtils#messagingAwareMapper(String...)}.
* <p>
* <b>Note:</b> The same ObjectMapper configuration should be used in the corresponding
* {@link JacksonMessageRowMapper} for consistent serialization and deserialization.
* @param objectMapper the {@link ObjectMapper} to use for JSON serialization
*/
public JacksonChannelMessageStorePreparedStatementSetter(ObjectMapper objectMapper) {
super();
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}

@Override
public void setValues(PreparedStatement preparedStatement, Message<?> requestMessage,
Object groupId, String region, boolean priorityEnabled) throws SQLException {

super.setValues(preparedStatement, requestMessage, groupId, region, priorityEnabled);

try {
String json = this.objectMapper.writeValueAsString(requestMessage);

String dbProduct = preparedStatement.getConnection().getMetaData().getDatabaseProductName();

if ("PostgreSQL".equalsIgnoreCase(dbProduct)) {
preparedStatement.setObject(6, json, Types.OTHER); // NOSONAR magic number
}
else {
preparedStatement.setString(6, json); // NOSONAR magic number
}
}
catch (JacksonException ex) {
throw new SQLException("Failed to serialize message to JSON: " + requestMessage, ex);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2025-present 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.integration.jdbc.store.channel;

import java.sql.ResultSet;
import java.sql.SQLException;

import tools.jackson.core.JacksonException;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;

import org.springframework.integration.support.json.JacksonMessagingUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;

/**
* A {@link RowMapper} implementation that deserializes {@link Message} objects from
* JSON format stored in the database.
* <p>
* This mapper works in conjunction with {@link JacksonChannelMessageStorePreparedStatementSetter}
* to provide JSON serialization for Spring Integration's JDBC Channel Message Store.
* <p>
* Unlike the default {@link MessageRowMapper} which uses Java serialization,
* this implementation uses Jackson to deserialize JSON strings from the MESSAGE_CONTENT column.
* <p>
* The {@link ObjectMapper} is configured using {@link JacksonMessagingUtils#messagingAwareMapper(String...)}
* which validates all deserialized classes against a trusted package list to prevent
* security vulnerabilities.
* <p>
* <b>Usage Example:</b>
* <pre>{@code
* &#64;Bean
* JdbcChannelMessageStore messageStore(DataSource dataSource) {
* JdbcChannelMessageStore store = new JdbcChannelMessageStore(dataSource);
* store.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider());
*
* // Enable JSON serialization
* store.setPreparedStatementSetter(
* new JacksonChannelMessageStorePreparedStatementSetter());
* store.setMessageRowMapper(
* new JacksonMessageRowMapper("com.example"));
*
* return store;
* }
* }</pre>
*
* @author Yoobin Yoon
*
* @since 7.0
*/
public class JacksonMessageRowMapper implements RowMapper<Message<?>> {

private final ObjectMapper objectMapper;

/**
* Create a new {@link JacksonMessageRowMapper} with additional trusted packages
* for deserialization.
* <p>
* The provided packages are appended to the default trusted packages from
* {@link JacksonMessagingUtils#DEFAULT_TRUSTED_PACKAGES}, enabling deserialization
* of custom payload types while maintaining security.
* @param trustedPackages the additional packages to trust for deserialization
*/
public JacksonMessageRowMapper(String... trustedPackages) {
this.objectMapper = JacksonMessagingUtils.messagingAwareMapper(trustedPackages);
}

/**
* Create a new {@link JacksonMessageRowMapper} with a custom {@link ObjectMapper}.
* <p>
* This constructor allows full control over the JSON deserialization configuration.
* The provided mapper should be configured appropriately for Message deserialization,
* typically using {@link JacksonMessagingUtils#messagingAwareMapper(String...)}.
* <p>
* <b>Note:</b> The same ObjectMapper configuration should be used in the corresponding
* {@link JacksonChannelMessageStorePreparedStatementSetter} for consistent
* serialization and deserialization.
* @param objectMapper the {@link ObjectMapper} to use for JSON deserialization
*/
public JacksonMessageRowMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}

@Override
public Message<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
try {
String json = rs.getString("MESSAGE_CONTENT");

if (json == null) {
throw new SQLException("MESSAGE_CONTENT column is null at row " + rowNum);
}

return this.objectMapper.readValue(json, new TypeReference<Message<?>>() {

});
}
catch (JacksonException ex) {
throw new SQLException(
"Failed to deserialize message from JSON at row " + rowNum + ". "
+ "Ensure the JSON was created by JacksonChannelMessageStorePreparedStatementSetter "
+ "and contains proper @class type information.",
ex);
}
}

}
Loading