Search code examples
spring-bootspring-cloud-streamspring-rabbit

Conversion by Spring Cloud Stream of Message with pojo on consumer side returns object with null fields


Here is code snippets to demonstrate the problem. On producer side:

    ProductModelDto dto = new ProductModelDto(1L, "name", "desc", 100.5);
    streamBridge.send(PRODUCTS_OUT, MessageBuilder.withPayload(dto).build());

On consumer side:

    @Bean
    public Consumer<Message<ProductModelDto>> productCreated() {
        return message -> {
            log.info("payload = {}", message.getPayload());
            log.info("payload class = {}", message.getPayload().getClass().getName());
            log.info("sourceData = {}", message.getHeaders().get("sourceData"));
        };
    }

Output:

payload = ProductModelDto(id=null, name=null, description=null, price=null)
payload class = ru.security.common.model.product.ProductModelDto
sourceData = (Body:'{"id":1,"name":"name","description":"desc","price":100.5}' MessageProperties [headers={}, timestamp=Tue Sep 07 11:13:02 MSK 2021, messageId=3840075f-1142-f94d-be37-7be950d73f54, contentType=application/json, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=product-service.product-created-or-changed, receivedRoutingKey=product-service.product-created-or-changed, deliveryTag=1, consumerTag=amq.ctag-3i5ECkRTPs5_O5ZW5af-MA, consumerQueue=product-service.product-created-or-changed.some-group4])

I expect to receive payload which have been sent by producer. But resulted payload is ProductModelDto(id=null, name=null, description=null, price=null). I know that spring automatically convert message to pojo if I use

Consumer<ProductModelDto> productCreated()

but I need

Consumer<Message<ProductModelDto>> productCreated()

to get Headers from message. Any suggestion where I miss some configuration?


Solution

  • I've created sample project and found out that this issue is reproduced in version 3.0.11.RELEASE of spring-cloud-starter-stream-rabbit. In versions 3.1.4 or 3.0.10.RELEASE i didn't see this problem. https://github.com/serjteplov/demo-gitter-scs1.git