-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Jackson JSON serialization for JdbcChannelMessageStore #10508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+1,088
−7
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...mework/integration/jdbc/store/channel/JsonChannelMessageStorePreparedStatementSetter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.SQLException; | ||
| import java.sql.Types; | ||
|
|
||
| import org.springframework.integration.support.json.JacksonJsonObjectMapper; | ||
| import org.springframework.integration.support.json.JacksonMessagingUtils; | ||
| import org.springframework.integration.support.json.JsonObjectMapper; | ||
| import org.springframework.messaging.Message; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| /** | ||
| * A {@link ChannelMessageStorePreparedStatementSetter} implementation that uses JSON | ||
| * serialization for {@link Message} objects instead of Java serialization. | ||
| * <p> | ||
| * By default, this implementation stores the entire message (including headers and payload) as JSON, | ||
| * with type information embedded using Jackson's {@code @class} property for proper deserialization. | ||
| * <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> | ||
| * <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 | ||
| * @Bean | ||
| * JdbcChannelMessageStore messageStore(DataSource dataSource) { | ||
| * JdbcChannelMessageStore store = new JdbcChannelMessageStore(dataSource); | ||
| * store.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider()); | ||
| * | ||
| * // Enable JSON serialization (requires schema modification) | ||
| * store.setPreparedStatementSetter( | ||
| * new JsonChannelMessageStorePreparedStatementSetter()); | ||
| * store.setMessageRowMapper( | ||
| * new JsonMessageRowMapper("com.example")); | ||
| * | ||
| * return store; | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * @author Yoobin Yoon | ||
| * | ||
| * @since 7.0 | ||
| */ | ||
| public class JsonChannelMessageStorePreparedStatementSetter extends ChannelMessageStorePreparedStatementSetter { | ||
|
|
||
| private final JsonObjectMapper<?, ?> jsonObjectMapper; | ||
|
|
||
| /** | ||
| * Create a new {@link JsonChannelMessageStorePreparedStatementSetter} with the | ||
| * default {@link JsonObjectMapper} configured for Spring Integration message serialization. | ||
| * <p> | ||
| * This constructor is suitable when serializing standard Spring Integration | ||
| * and Java classes. Custom payload types will require their package to be added to the | ||
| * corresponding {@link JsonMessageRowMapper}. | ||
| */ | ||
| public JsonChannelMessageStorePreparedStatementSetter() { | ||
| this(new JacksonJsonObjectMapper(JacksonMessagingUtils.messagingAwareMapper())); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link JsonChannelMessageStorePreparedStatementSetter} with a | ||
| * custom {@link JsonObjectMapper}. | ||
| * <p> | ||
| * This constructor allows full control over the JSON serialization configuration. | ||
| * <p> | ||
| * <b>Note:</b> The same JsonObjectMapper configuration should be used in the corresponding | ||
| * {@link JsonMessageRowMapper} for consistent serialization and deserialization. | ||
| * @param jsonObjectMapper the {@link JsonObjectMapper} to use for JSON serialization | ||
| */ | ||
| public JsonChannelMessageStorePreparedStatementSetter(JsonObjectMapper<?, ?> jsonObjectMapper) { | ||
| super(); | ||
| Assert.notNull(jsonObjectMapper, "'jsonObjectMapper' must not be null"); | ||
| this.jsonObjectMapper = jsonObjectMapper; | ||
| } | ||
|
|
||
| @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.jsonObjectMapper.toJson(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 (IOException ex) { | ||
| throw new SQLException("Failed to serialize message to JSON: " + requestMessage, ex); | ||
| } | ||
| } | ||
|
|
||
| } |
120 changes: 120 additions & 0 deletions
120
...rc/main/java/org/springframework/integration/jdbc/store/channel/JsonMessageRowMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import org.springframework.integration.support.json.JacksonJsonObjectMapper; | ||
| import org.springframework.integration.support.json.JacksonMessagingUtils; | ||
| import org.springframework.integration.support.json.JsonObjectMapper; | ||
| 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 JsonChannelMessageStorePreparedStatementSetter} | ||
| * 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 JSON to deserialize message strings from the MESSAGE_CONTENT column. | ||
| * <p> | ||
| * The underlying {@link JsonObjectMapper} validates all deserialized classes against a | ||
| * trusted package list to prevent security vulnerabilities. | ||
| * <p> | ||
| * <b>Usage Example:</b> | ||
| * <pre>{@code | ||
| * @Bean | ||
| * JdbcChannelMessageStore messageStore(DataSource dataSource) { | ||
| * JdbcChannelMessageStore store = new JdbcChannelMessageStore(dataSource); | ||
| * store.setChannelMessageStoreQueryProvider(new PostgresChannelMessageStoreQueryProvider()); | ||
| * | ||
| * // Enable JSON serialization | ||
| * store.setPreparedStatementSetter( | ||
| * new JsonChannelMessageStorePreparedStatementSetter()); | ||
| * store.setMessageRowMapper( | ||
| * new JsonMessageRowMapper("com.example")); | ||
| * | ||
| * return store; | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * @author Yoobin Yoon | ||
| * | ||
| * @since 7.0 | ||
| */ | ||
| public class JsonMessageRowMapper implements RowMapper<Message<?>> { | ||
|
|
||
| private final JsonObjectMapper<?, ?> jsonObjectMapper; | ||
|
|
||
| /** | ||
| * Create a new {@link JsonMessageRowMapper} with additional trusted packages | ||
| * for deserialization. | ||
| * <p> | ||
| * The provided packages are appended to the default trusted packages, | ||
| * enabling deserialization of custom payload types while maintaining security. | ||
| * If no packages are provided, only the default trusted packages are used. | ||
| * @param trustedPackages the additional packages to trust for deserialization. | ||
| * Can be {@code null} or empty to use only default packages | ||
| */ | ||
| public JsonMessageRowMapper(String @Nullable ... trustedPackages) { | ||
| this(new JacksonJsonObjectMapper( | ||
| JacksonMessagingUtils.messagingAwareMapper(trustedPackages))); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link JsonMessageRowMapper} with a custom {@link JsonObjectMapper}. | ||
| * <p> | ||
| * This constructor allows full control over the JSON deserialization configuration. | ||
| * <p> | ||
| * <b>Note:</b> The same JsonObjectMapper configuration should be used in the corresponding | ||
| * {@link JsonChannelMessageStorePreparedStatementSetter} for consistent | ||
| * serialization and deserialization. | ||
| * @param jsonObjectMapper the {@link JsonObjectMapper} to use for JSON deserialization | ||
| */ | ||
| public JsonMessageRowMapper(JsonObjectMapper<?, ?> jsonObjectMapper) { | ||
| Assert.notNull(jsonObjectMapper, "'jsonObjectMapper' must not be null"); | ||
| this.jsonObjectMapper = jsonObjectMapper; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("NullAway") | ||
| 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.jsonObjectMapper.fromJson(json, Message.class); | ||
| } | ||
| catch (IOException ex) { | ||
| throw new SQLException( | ||
| "Failed to deserialize message from JSON at row " + rowNum + ". " | ||
| + "Ensure the JSON and the configured JsonObjectMapper use compatible type handling.", | ||
| ex); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.