| New file | 
|  |  |  | 
|---|
|  |  |  | package com.moral.util; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.UnsupportedEncodingException; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.amqp.core.Message; | 
|---|
|  |  |  | import org.springframework.amqp.core.MessageProperties; | 
|---|
|  |  |  | import org.springframework.amqp.support.converter.AbstractMessageConverter; | 
|---|
|  |  |  | import org.springframework.amqp.support.converter.MessageConversionException; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson.TypeReference; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class MessageConverter extends AbstractMessageConverter { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static final String DEFAULT_CHARSET = "UTF-8"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException { | 
|---|
|  |  |  | byte[] bytes = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String jsonString = JSON.toJSONString(objectToConvert); | 
|---|
|  |  |  | bytes = jsonString.getBytes(DEFAULT_CHARSET); | 
|---|
|  |  |  | } catch (UnsupportedEncodingException e) { | 
|---|
|  |  |  | throw new MessageConversionException("Failed to convert Message content", e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON); | 
|---|
|  |  |  | messageProperties.setContentEncoding(DEFAULT_CHARSET); | 
|---|
|  |  |  | if (bytes != null) { | 
|---|
|  |  |  | messageProperties.setContentLength(bytes.length); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new Message(bytes, messageProperties); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Object fromMessage(Message msg) throws MessageConversionException { | 
|---|
|  |  |  | String message = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | message = new String(msg.getBody(), DEFAULT_CHARSET); | 
|---|
|  |  |  | } catch (UnsupportedEncodingException e) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return JSON.parseObject(message, new TypeReference<Map<String, Object>>() {}); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|