1 package de.example.spring.kafka;
3 import org.springframework.messaging.converter.MappingJackson2MessageConverter;
4 import org.springframework.util.MimeType;
7 * Working around problem created by org.springframework.cloud.stream.binder.AbstractBinder.JavaClassMimeTypeConversion.mimeTypeFromObject()
10 * if (payload instanceof String) {
11 * return MimeTypeUtils.APPLICATION_JSON_VALUE.equals(originalContentType) ? MimeTypeUtils.APPLICATION_JSON
12 * : MimeTypeUtils.TEXT_PLAIN;
15 * Changes messages from:
16 * contentType "application/json;charset=UTF-8"{"name":"example message","description":"this is some description"}
19 * contentType "text/plain" originalContentType "application/json;charset=UTF-8"{"name":"example message","description":"this is some description"}
22 * By means of my custom converter we end up having:
24 * contentType "application/json"{"name":"example message","description":"this is some description"}
26 * About "application/json" and character encoding:
27 * https://tools.ietf.org/html/rfc7158#section-8.1 "The default encoding is UTF-8" :)
35 // You should set breakpoints in org.springframework.cloud.stream.binder.AbstractBinder.deserializePayload
36 // org.springframework.cloud.stream.binder.AbstractBinder.serializePayloadIfNecessary
37 // org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.SendingHandler.handleMessageInternal
39 // The code in this Spring project is a bit messy (IMHO) for example this.embedHeaders in
40 // SendingHandler.handleMessageInternal: When the heck this.embedHeaders must do something? Depending on
41 // the content type embedHeaders will be doing something or not. This is weird. Also deserializePayload is
42 // always using UTF-8. It does not matter what character set I am receiving :/
47 // Be careful when using Spring Cloud Stream because there could be surprises when trying to connect to systems
48 // not using Spring (Spring is creating a mess with the headers...)
55 // Kafka messages WITHOUT MyCustomMessageConverter:
57 // contentType "text/plain" <--------------- perhaps this is important.
58 // originalContentType "application/json;charset=UTF-8"
60 // {"name":"example message","description":"this is some description"}
63 // Kafka messages WITH MyCustomMessageConverter:
65 // contentType "application/json"
67 // {"name":"example message","description":"this is some description"}
71 // YOU'D RATHER BETTER NOT USE THIS CONVERTER BECAUSE I DO NOT KNOW IF contentType "text/plain" IS IMPORTANT OR NOT :(
72 public class MyCustomMessageConverter extends MappingJackson2MessageConverter {
74 public MyCustomMessageConverter() {
75 super(new MimeType("application", "json"));