相关文章推荐
睡不着的橡皮擦  ·  IllegalArgumentExcepti ...·  3 周前    · 
憨厚的烈酒  ·  golang处理json转义符 ...·  6 月前    · 
淡定的槟榔  ·  FormatMessageW 函数 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to convert received message from mqtt-servet to an java object. I couldn't find a hint how it is possible with spring-integration tools. Here is the code

@Bean
  public MqttPahoClientFactory mqttClientFactory() {
    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    MqttConnectOptions options = new MqttConnectOptions();
    options.setServerURIs(new String[] 
      "ssl://"+this.hostname+":"+this.port, 
    options.setUserName(this.username);
    options.setPassword(this.password.toCharArray());
    factory.setConnectionOptions(options);
    return factory;
@Bean
  public MessageProducer inboundSendorData() {
    String clientId = "Java_" + UUID.randomUUID().toString();
    MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
      clientId,
      this.mqttClientFactory(),
      "sensordata"
    //DefaultPahoMessageConverter converter = new DefaultPahoMessageConverter();
    //adapter.setConverter(converter);
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    adapter.setOutputChannel(mqttInputChannelSensorData());
    return adapter;
  @Bean
  public MessageChannel mqttInputChannelSensorData() {
      return new DirectChannel();
  @Bean
  @ServiceActivator(inputChannel = "mqttInputChannelSensorData")
  public MessageHandler handlerSensorData() {
    return new MqttSubSensorHandler();

Here is the code for the Handler

public class MqttSubSensorHandler implements MessageHandler {
  @Autowired
  private SensorRepository sensorRepository;
  @Autowired
  public MqttSubSensorHandler(SensorRepository sensorRepository) {
    this.sensorRepository = sensorRepository;
  public MqttSubSensorHandler() {
  public void handleResponse(Message<?> message) {
  @Override
  public void handleMessage(Message<?> message) throws MessagingException {
    System.out.println(message.getPayload());

I assume it is possible, because it is possible with Spring JMS. And the approach for setting up the JMS connection is pretty similar.

Thanks I have tried it out. But it didn't worked for me. For some reason ConvertingBytesMessageMapper didn't the received Message to fromMessage() of MappingJackson2MessageConverter. At the moment I have ended up to convert the message in the MessageHandler with Jackson's ObjectMapper – KirillS Jan 28, 2021 at 20:46 Huh? We are talking about from MQTT to POJO, so that has to be MappingJackson2MessageConverter.toMessage(). It would be great if you debug and confirm why that doesn't work for you... – Artem Bilan Jan 28, 2021 at 20:58

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.