Spring Messaging usage of Jackson to Serialize Outgoing Messages

From NovaOrdis Knowledge Base
Revision as of 21:46, 14 November 2018 by Ovidiu (talk | contribs) (Created page with "<syntaxhighlight lang='java'> package org.springframework.messaging.converter; public class MappingJackson2MessageConverter extends AbstractMessageConverter { ... @Over...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
package org.springframework.messaging.converter;

public class MappingJackson2MessageConverter extends AbstractMessageConverter {

  ...

  @Override
  @Nullable
  protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers,	@Nullable Object conversionHint) {
    try {
      Class<?> view = getSerializationView(conversionHint);
      if (byte[].class == getSerializedPayloadClass()) {
       ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
       JsonEncoding encoding = getJsonEncoding(getMimeType(headers));
       JsonGenerator generator = this.objectMapper.getFactory().createGenerator(out, encoding);
       if (view != null) {
         this.objectMapper.writerWithView(view).writeValue(generator, payload);
       }
       else {
         this.objectMapper.writeValue(generator, payload);
       }
       payload = out.toByteArray();
     }
     else {
       Writer writer = new StringWriter();
       if (view != null) {
         this.objectMapper.writerWithView(view).writeValue(writer, payload);
       }
       else {
         this.objectMapper.writeValue(writer, payload);
       }
       payload = writer.toString();
    }
  }
  catch (IOException ex) {
    throw new MessageConversionException("Could not write JSON: " + ex.getMessage(), ex);
  }
  return payload;
}