我得到如下所示的消息,其中包含2個HashMap(inputMap和outputMap)。這是使用ObjectMapper的正確方法嗎?如何將消息直接綁定到我的對象類
示例消息:
在RabbitMQ的:
{"ORCH_KEY":{"inputMap":{},"outputMap":{"activityId":"10001002","activityStatus":"SUCCESS"}}}
在配置代碼
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
Jackson2JsonMessageConverter messageConverter=new Jackson2JsonMessageConverter();
DefaultClassMapper classMapper = new DefaultClassMapper();
classMapper.setDefaultType(ExchangeDTO.class);
messageConverter.setClassMapper(classMapper);
factory.setMessageConverter(messageConverter);
消費者法典
@RabbitListener(containerFactory = "adapterOPListenerContainerFactory", queues = Constants.ADAPTOR_OP_QUEUE)
public void handleAdapterQueueMessage(HashMap<String, ExchangeDTO> adapterOutputMap) {
logger.info("Adapter Handler::::::::::"+this.getClass().getCanonicalName());
try {
logger.info("Adapter Output Map:::::::::::" + adapterOutputMap);
if(adapterOutputMap.size()==1){
Iterator<Entry<String, ExchangeDTO>> iterator = adapterOutputMap.entrySet().iterator();
Entry<String, ExchangeDTO> next = iterator.next();
String ORCH_KEY =next.getKey();
logger.info("Ochestration Key:::::::::::" + ORCH_KEY);
ExchangeDTO exchangeDTO = next.getValue();
logger.info("Size of OutputMap:::::"+exchangeDTO.getOutputMap().size());
ExchangeDTO類
public class ExchangeDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private HashMap<String, Object> inputMap = new HashMap<String, Object>();
private HashMap<String, Object> outputMap = new HashMap<String, Object>();
public HashMap<String, Object> getInputMap() {
return inputMap;
}
public void setInputMap(HashMap<String, Object> inputMap) {
this.inputMap = inputMap;
}
public HashMap<String, Object> getOutputMap() {
return outputMap;
}
public void setOutputMap(HashMap<String, Object> outputMap) {
this.outputMap = outputMap;
}
}
我面臨着這樣的錯誤如下:
java.util.LinkedHashMap中不能轉換到 com.bosch.bip.dto.ExchangeDTO
您是否試圖錯誤地將{inputMap = {},outputMap = {activityId = 10001002,activityStatus = SUCCESS}}解析爲JSON,錯誤似乎指向了這一點。 – Ironluca