Skip to content

NullPointerException in SimpleAmqpHeaderMapper when message priority is null #3246

@daniloarcidiacono

Description

@daniloarcidiacono

In what version(s) of Spring AMQP are you seeing this issue?

4.0.0

Describe the bug

I encountered a NullPointerException in SimpleAmqpHeaderMapper when receiving messages that do not have the priority property explicitly set (i.e., when priority is null). This appears to be a regression in 4.0.0.

The stack trace points to an unboxing issue during header mapping:

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "priority" is null
	at org.springframework.amqp.support.SimpleAmqpHeaderMapper.toHeaders(SimpleAmqpHeaderMapper.java:155) ~[spring-amqp-4.0.0.jar:4.0.0]
	at org.springframework.amqp.support.SimpleAmqpHeaderMapper.toHeaders(SimpleAmqpHeaderMapper.java:57) ~[spring-amqp-4.0.0.jar:4.0.0]

Looking at the source for SimpleAmqpHeaderMapper.java (around line 155), the code attempts to compare a nullable Integer to a primitive int:

Integer priority = amqpMessageProperties.getPriority(); // Returns null if not set

// The condition (priority > 0) causes implicit unboxing of null, throwing NPE
javaUtils.acceptIfCondition(priority > 0, AmqpMessageHeaderAccessor.PRIORITY, priority, putObject)
         .acceptIfNotNull(...)

Comparison with previous versions:
In spring-amqp:2.4.3, this logic was null-safe:

Integer priority = amqpMessageProperties.getPriority();
javaUtils.acceptIfCondition(priority != null && priority > 0, ...);

In 4.0.0, the null check was removed, leading to the regression:

Integer priority = amqpMessageProperties.getPriority();
javaUtils.acceptIfCondition(priority > 0, ...); // NPE if priority is null

To Reproduce

  1. Create a Spring Boot 4 application with spring-boot-starter-amqp.
  2. Define a standard @RabbitListener.
  3. Publish a message to the queue using RabbitTemplate (or the Management UI) without setting a specific priority property (leaving it as the default/null).
  4. The listener container logs an exception and fails to process the message.

Expected behavior

The application should consume the message without error, treating the missing priority as null or 0, consistent with previous versions of Spring AMQP. It should not attempt to unbox a null Integer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions