Skip to content

Commit 56a6800

Browse files
Clarify message conversion docs and javadocs (#1550) (#1560)
Docs and javadocs changes only, no code or behavior changes. Closes #1550
1 parent ea1396d commit 56a6800

File tree

5 files changed

+74
-37
lines changed

5 files changed

+74
-37
lines changed

docs/src/main/asciidoc/sqs.adoc

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,14 +1647,47 @@ public SqsMessageListenerContainerFactory<Object> defaultSqsListenerContainerFac
16471647
----
16481648
=== Message Conversion and Payload Deserialization
16491649

1650-
Payloads are automatically deserialized from `JSON` for `@SqsListener` annotated methods using a `JacksonJsonMessageConverter` when Jackson 3 is on classpath. If there is no Jackson 3 on classpath and there is Jackson 2 on classpath `MappingJackson2MessageConverter` will be used.
1650+
Payloads are automatically deserialized from `JSON` for `@SqsListener` annotated methods to the inferred payload type. See <<Automatic Payload Type Inference>> for information on how the payload type is inferred.
16511651

1652-
NOTE: When using Spring Boot's auto-configuration, if there's a single `JsonMapper` in Spring Context, such object mapper will be used for converting messages.
1653-
This includes the one provided by Spring Boot's auto-configuration itself.
1654-
For configuring a different `JsonMapper`, see <<Global Configuration for @SqsListeners>>.
1652+
==== Configuring Payload Deserialization
16551653

1656-
NOTE: When Jackson 3 is not on classpath and only Jackson 2 is found, if there's a single `ObjectMapper` in Spring Context, such object mapper will be used for converting messages.
1657-
This includes the one provided by Spring Boot's auto-configuration itself.
1654+
Payload conversion for `@SqsListener` methods is performed by a `SqsMessagingMessageConverter` by default.
1655+
1656+
If message conversion customization is necessary, the `SqsMessagingMessageConverter` provided by auto-configuration can be overridden by declaring a `MessagingMessageConverter` bean, or by configuring it through `SqsMessageListenerContainerFactory` and its `ContainerOptions`.
1657+
1658+
Customizing via `ContainerOptions`:
1659+
1660+
[source,java]
1661+
----
1662+
@Bean
1663+
SqsMessageListenerContainerFactory<?> sqsMessageListenerContainerFactory(MessagingMessageConverter<?> converter) {
1664+
SqsMessageListenerContainerFactory<?> factory = new SqsMessageListenerContainerFactory<>();
1665+
factory.configure(options -> options.messageConverter(converter));
1666+
return factory;
1667+
}
1668+
----
1669+
1670+
Providing a custom `SqsMessagingMessageConverter` backed by a `JsonMapper`:
1671+
1672+
[source,java]
1673+
----
1674+
@Bean
1675+
MessagingMessageConverter<?> messageConverter(JsonMapper jsonMapper) {
1676+
return new SqsMessagingMessageConverter(jsonMapper);
1677+
}
1678+
----
1679+
1680+
NOTE: When using Spring Boot's auto-configuration, if there is a single `JsonMapper` in the Spring context, that `JsonMapper` will be used for converting messages. This includes the one provided by Spring Boot's auto-configuration itself.
1681+
1682+
==== Jackson Migration
1683+
1684+
During the Jackson migration period, if Jackson 3 is on the classpath, a `JacksonJsonMessageConverter` will be used.
1685+
If only Jackson 2 is on the classpath, `MappingJackson2MessageConverter` will be used.
1686+
1687+
When using Jackson 2, if there is a single `ObjectMapper` in the Spring context, that `ObjectMapper` will be used for converting messages.
1688+
1689+
While auto-selection should be sufficient for most users, if both Jackson 2 and Jackson 3 are present on the classpath and Jackson 2-based conversion is required, a temporary workaround is to declare a `LegacyJackson2SqsMessagingMessageConverter` bean for payload deserialization and/or a `LegacyJackson2MessageConverterMigration` bean for the default listener method argument resolvers.
1690+
These workarounds are transitional and not a long-term supported contract.
16581691

16591692
==== Automatic Payload Type Inference
16601693

@@ -2050,26 +2083,16 @@ The following attributes can be configured in the registrar:
20502083
- `setMessageHandlerMethodFactory` - provide a different factory to be used to create the `invocableHandlerMethod` instances that wrap the listener methods.
20512084
- `setListenerContainerRegistry` - provide a different `MessageListenerContainerRegistry` implementation to be used to register the `MessageListenerContainers`
20522085
- `setMessageListenerContainerRegistryBeanName` - provide a different bean name to be used to retrieve the `MessageListenerContainerRegistry`
2053-
- `setJacksonMessageConverterMigration` - set the `JacksonMessageConverterMigration` which will be used to construct `MessageConverter` and provide either `JsonMapper` for Jackson 3 or `ObjectMapper` for Jackson 2. Check `JacksonJsonMessageConverterMigration` and `LegacyJackson2MessageConverterMigration`
2054-
See <<Message Conversion and Payload Deserialization>> for more information on where this is used.
2086+
- `setJacksonMessageConverterMigration` - internal migration wiring for the Jackson 2 to Jackson 3 transition, not intended for application configuration.
20552087
- `setValidator` - set the `Validator` instance that will be used for payload validation in listener methods.
2056-
- `manageMessageConverters` - gives access to the list of message converters that will be used to convert messages.
2088+
- `manageMessageConverters` - gives access to the list of message converters that will be used by ArgumentResolvers.
20572089
By default, `StringMessageConverter`, `SimpleMessageConverter` and `JacksonJsonMessageConverter` are used.
20582090

20592091
- `manageArgumentResolvers` - gives access to the list of argument resolvers that will be used to resolve the listener method arguments.
2060-
The order of resolvers is important - `PayloadMethodArgumentResolver` should generally be last since it's used as default.
20612092
- `setMethodPayloadTypeInferrer` - set the `MethodPayloadTypeInferrer` instance to be used for automatic payload type inference.
20622093
Set to `null` to disable automatic inference and rely on header-based type mapping.
20632094
See <<Automatic Payload Type Inference>> for more information.
20642095

2065-
A simple example for Jackson 3 would be:
2066-
2067-
[source, java]
2068-
----
2069-
@Bean
2070-
SqsListenerConfigurer configurer(JacksonJsonMessageConverterFactory factory) {
2071-
return registrar -> registrar.setJacksonMessageConverterFactory(factory);
2072-
}
20732096
----
20742097
20752098
NOTE: Any number of `SqsListenerConfigurer` beans can be registered in the context.

spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/config/SqsListenerConfigurer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ public interface SqsListenerConfigurer {
2929

3030
/**
3131
* Configures the {@link EndpointRegistrar} instance that will handle the {@link Endpoint} instances.
32+
*
33+
* <p>Example:
34+
* <pre class="code">
35+
* &#64;Bean
36+
* SqsListenerConfigurer sqsListenerConfigurer() {
37+
* return registrar -> registrar.manageArgumentResolvers(resolvers -> {
38+
* resolvers.add(new MyCustomArgumentResolver());
39+
* });
40+
* }
41+
* </pre>
42+
*
3243
* @param registrar the registrar instance.
3344
*/
3445
void configure(EndpointRegistrar registrar);

spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/support/converter/legacy/JacksonJsonMessageConverterMigration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
*/
1616
package io.awspring.cloud.sqs.support.converter.legacy;
1717

18-
import io.awspring.cloud.sqs.annotation.SqsListenerAnnotationBeanPostProcessor;
1918
import io.awspring.cloud.sqs.config.MessageConverterFactory;
2019
import io.awspring.cloud.sqs.support.resolver.NotificationMessageArgumentResolver;
2120
import io.awspring.cloud.sqs.support.resolver.NotificationSubjectArgumentResolver;
2221
import io.awspring.cloud.sqs.support.resolver.SnsNotificationArgumentResolver;
2322
import java.util.List;
24-
import org.springframework.messaging.converter.JacksonJsonMessageConverter;
2523
import org.springframework.messaging.converter.MessageConverter;
2624
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
2725
import tools.jackson.databind.json.JsonMapper;
2826

2927
/**
30-
* Used to create {@link JacksonJsonMessageConverter} and provide {@link JsonMapper} to
31-
* {@link SqsListenerAnnotationBeanPostProcessor}.
28+
* Internal, migration-only implementation for the Jackson 3 variant.
29+
* <p>
30+
* This type is transitional and will be removed after the Jackson 3 migration is complete. It is internal
31+
* wiring and not intended to be used by application code.
3232
*
3333
* @author Matej Nedic
3434
* @since 4.0.0

spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/support/converter/legacy/JacksonMessageConverterMigration.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,35 @@
2222
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
2323

2424
/**
25-
* Converter factory used to create MessageConverter either for Jackson 2 or Jackson 3. Used also to provide
26-
* Implementation for Jackson 3 {@link JacksonJsonMessageConverterMigration} Implementation for Jackson 2
27-
* {@link LegacyJackson2MessageConverterMigration} Note that you can declare either of implementations as a Bean which
28-
* will be picked by autoconfiguration. NOTE this is transition only api.
25+
* Internal, migration-only temporary contract used by the framework to bridge Jackson 2 and Jackson 3.
26+
* <p>
27+
* This type is transitional and will be removed after the Jackson 3 migration is complete. It is internal
28+
* wiring and implementations are not supported outside the migration.
29+
* <p>
30+
* To customize payload conversion, provide a {@link MessagingMessageConverter} (such as
31+
* {@link io.awspring.cloud.sqs.support.converter.SqsMessagingMessageConverter}) bean.
32+
* To customize listener method argument resolvers, use {@link SqsListenerConfigurer}.
33+
*
2934
* @author Matej Nedic
3035
* @since 4.0.0
3136
*/
3237
@Deprecated(forRemoval = true)
3338
public interface JacksonMessageConverterMigration {
3439

3540
/**
36-
* Creates migration MessageConverter, either Jackson 3 or Jackson 2 specific.
37-
* @return MessageConverter used by SQS integration
41+
* @return the migration {@link MessageConverter}
3842
*/
3943
MessageConverter createMigrationMessageConverter();
4044

4145
/**
42-
* Used by {@link SqsListenerConfigurer} to add resolvers which will be used when resolving message.
43-
* @param argumentResolvers List of argument resolvers
44-
* @param messageConverter MessageConverters which will be used by resolvers.
46+
* @param argumentResolvers list to add migration resolvers to
47+
* @param messageConverter migration {@link MessageConverter} to be used by those resolvers
4548
*/
4649
void addJacksonMigrationResolvers(List<HandlerMethodArgumentResolver> argumentResolvers,
47-
MessageConverter messageConverter);
50+
MessageConverter messageConverter);
4851

4952
/**
50-
* Used to enrich {@link MessagingMessageConverter} with Jackson implementation
51-
* @param messageConverter Which will be enriched
53+
* @param messageConverter converter to configure as part of the legacy migration path
5254
*/
5355
default void configureLegacyObjectMapper(MessagingMessageConverter messageConverter) {
5456
}

spring-cloud-aws-sqs/src/main/java/io/awspring/cloud/sqs/support/converter/legacy/LegacyJackson2MessageConverterMigration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package io.awspring.cloud.sqs.support.converter.legacy;
1717

1818
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import io.awspring.cloud.sqs.annotation.SqsListenerAnnotationBeanPostProcessor;
2019
import io.awspring.cloud.sqs.config.legacy.LegacyJackson2MessageConverterFactory;
2120
import io.awspring.cloud.sqs.support.converter.MessagingMessageConverter;
2221
import io.awspring.cloud.sqs.support.resolver.legacy.LegacyJackson2NotificationMessageArgumentResolver;
@@ -26,8 +25,10 @@
2625
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
2726

2827
/**
29-
* Used to create {@link LegacyJackson2MessageConverterMigration} and provide ObjectMapper to
30-
* {@link SqsListenerAnnotationBeanPostProcessor}.
28+
* Internal, migration-only implementation for the Jackson 2 variant.
29+
* <p>
30+
* This type is transitional and will be removed after the Jackson 3 migration is complete.
31+
*
3132
* @author Matej Nedic
3233
* @since 4.0.0
3334
*/

0 commit comments

Comments
 (0)