Minor polishing on message converter doc

This fixes #616

Add example for MessageConverter
This commit is contained in:
Ilayaperumal Gopinathan
2016-09-16 11:56:00 +05:30
committed by markfisher
parent c9192c6c8e
commit 0d8bbc6665

View File

@@ -1007,7 +1007,7 @@ Spring Cloud Stream can handle messages based on this information in two ways:
* Through its `contentType` settings on inbound and outbound channels
* Through its argument mapping performed for methods annotated with `@StreamListener`
Spring Cloud Stream allows you to declaratively configure type conversion for inputs and outputs using the `content-type` property of a binding.
Spring Cloud Stream allows you to declaratively configure type conversion for inputs and outputs using the `spring.cloud.stream.bindngs.<channelName>.content-type` property of a binding.
Note that general type conversion may also be accomplished easily by using a transformer inside your application.
Currently, Spring Cloud Stream natively supports the following type conversions commonly used in streams:
@@ -1106,7 +1106,48 @@ For the conversion of inbound messages, especially when the target is a POJO, th
Besides the conversions that it supports out of the box, Spring Cloud Stream also supports registering your own message conversion implementations.
This allows you to send and receive data in a variety of custom formats, including binary, and associate them with specific `contentTypes`.
In order to do so, you can create a class that extends `AbstractMessageConverter`
Spring Cloud Stream registers all the beans of type `org.springframework.messaging.converter.MessageConverter` as custom message converters along with the out of the box message converters.
If your message converter needs to work with specific `content-type` and the target class (for both input and output), then the message converter needs to extend `org.springframework.messaging.converter.AbstractMessageConverter`.
For the conversion using @StreamListener, the message converter that implements `org.springframework.messaging.converter.MessageConverter` would be suffice.
Here is an example of creating a message converter bean (with the content-type application/bar) inside the Spring Cloud Stream application:
[source,java]
----
@EnableBinding(Sink.class)
@SpringBootApplication
public static class SinkApplication {
...
@Bean
public MessageConverter customMessageConverter() {
return new MyCustomMessageConverter();
}
----
[source,java]
----
public class MyCustomMessageConverter extends AbstractMessageConverter {
public MyCustomMessageConverter() {
super(new MimeType("application", "bar"));
}
@Override
protected boolean supports(Class<?> clazz) {
return (Bar.class == clazz);
}
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
Object payload = message.getPayload();
return (payload instanceof Bar ? payload : new Bar((byte[]) payload));
}
}
----
=== Schema-based message converters
@@ -1127,7 +1168,7 @@ If the target type of the conversion is a `GenericRecord`, then a schema must be
For using it, you can simply add it to the application context, optionally specifying one ore more `MimeTypes` to associate it with.
The default `MimeType` is `application/avro`.
Here is an example of configuring it in a processor application registering the Apache Avro, without a predefined schema:
Here is an example of configuring it in a sink application registering the Apache Avro MessageConverter, without a predefined schema:
[source,java]
----