Skip to content

NPE in JsonSerializer on template.send if key is null #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Shabin opened this issue May 22, 2018 · 1 comment
Closed

NPE in JsonSerializer on template.send if key is null #686

Shabin opened this issue May 22, 2018 · 1 comment
Milestone

Comments

@Shabin
Copy link

Shabin commented May 22, 2018

I just upgraded from 1.3.4.RELEASE to 2.1.6.RELEASE and on sending a message without any key I am getting NPE.

-java.lang.NullPointerException
	at org.springframework.kafka.support.serializer.JsonSerializer.serialize(JsonSerializer.java:134)
	at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:783)
	at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:768)
	at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:285)
	at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:348)
	at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:181)
	at com.itest.event.distributer.kafka.KafkaEventDistributor.notify(KafkaEventDistributor.java:226)

I am calling the default send() method.
template.send(topicName, eventdata);

I coudn't find any migration steps related to this.

My kafka template configuration:

<bean id="kafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate">
		<constructor-arg>
			<bean class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
				<constructor-arg>
					<map>
						<entry key="bootstrap.servers" value="${event.kafka.url}" />
						<entry key="key.serializer"
							value="org.springframework.kafka.support.serializer.JsonSerializer" />
						<entry key="value.serializer"
							value="com.itest.event.kafka.EventJsonSerializer" />
					</map>
				</constructor-arg>
			</bean>
		</constructor-arg>
	</bean>
@artembilan
Copy link
Member

I think this is a good catch and our problem that we don't have this there:

 if (data == null)
            return null;

I mean that is is normal in Kafka world to send null as for value, as well as for key.

Therefore this NPE has to be fixed.

Feel free to contribute it, @Shabin !

Thank you!

@artembilan artembilan added this to the 2.1.7 milestone May 22, 2018
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Jun 13, 2018
artembilan pushed a commit that referenced this issue Jun 13, 2018
Fixes #686

**Cherry-pick to 2.1.x, 2.0.x, 1.3.x**
artembilan pushed a commit that referenced this issue Jun 13, 2018
Fixes #686

**Cherry-pick to 2.1.x, 2.0.x, 1.3.x**

(cherry picked from commit 8dc1dcd)
artembilan pushed a commit that referenced this issue Jun 13, 2018
Fixes #686

**Cherry-pick to 2.1.x, 2.0.x, 1.3.x**

(cherry picked from commit 8dc1dcd)

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/JsonDeserializer.java
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/JsonSerializer.java
artembilan pushed a commit that referenced this issue Jun 13, 2018
Fixes #686

**Cherry-pick to 2.1.x, 2.0.x, 1.3.x**

(cherry picked from commit 8dc1dcd)

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/JsonDeserializer.java
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/JsonSerializer.java

(cherry picked from commit 02d4a45)
denis554 added a commit to denis554/spring-kafka that referenced this issue Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants