Skip to content

NPE in AbstractMessageSendingTemplate when headers are null and converter returns null [SPR-11653] #16276

@spring-projects-issues

Description

@spring-projects-issues

Anders Schuller opened SPR-11653 and commented

When AbstractMessageSendingTemplate.convertAndSend is called with null headers and a payload that the converter cannot convert (i.e. converter.toMessage also returns null), the code attempts to throw a MessageConversionException but a NullPointerException occurs instead as the exception message attempts to get the content type from the null headers.

The relevant bit of code from AbstractMessageSendingTemplate:

MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null;
Message<?> message = this.converter.toMessage(payload, messageHeaders);

if (message == null) {
	String payloadType = (payload != null) ? payload.getClass().getName() : null;
	throw new MessageConversionException("Unable to convert payload type '"
			+ payloadType + "', Content-Type=" + messageHeaders.get(MessageHeaders.CONTENT_TYPE)
			+ ", converter=" + this.converter, null);
}

This bit of code was added with this commit, which I believe means AbstractMessagingTemplate.convertSendAndReceive is also affected. The fix is probably as easy as making the content type a separate variable where the headers can be null-checked, similar to how payloadType is declared.


Affects: 4.0.2, 4.0.3

Issue Links:

Metadata

Metadata

Assignees

Labels

in: messagingIssues in messaging modules (jms, messaging)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions