我正在使用Spring-cloud-stream版本3.0.4
我正在编写一个JSON合并器,它侦听多个流,将JSON存储在状态存储中,然后对其进行合并以生成输出JSON。由于我的服务只是JSON聚合器,因此我不想将JSONS转换为Java对象。因此,我想做的是将上游服务中的JSON发布为JsonNode。合并并在下游主题上发布。
要将JsonNode添加为受信任的程序包,我已经在我的Application Class中声明了一个bean,如下所示。
@Bean
public KafkaHeaderMapper customKafkaHeaderMapper()
{
DefaultKafkaHeaderMapper kafkaHeaderMapper = new DefaultKafkaHeaderMapper();
kafkaHeaderMapper.addTrustedPackages("com.fasterxml.jackson.databind.node");
return kafkaHeaderMapper;
}
并将以下条目添加到我的application.yml
spring.application.name: stream-aggregator
spring.cloud.stream.bindings.formDataIn:
destination: form-data
contentType: application/json
spring.cloud.stream.kafka.streams.binder:
configuration:
default.key.serde: org.springframework.kafka.support.serializer.JsonSerde
default.value.serde: org.springframework.kafka.support.serializer.JsonSerde
spring.json.key.default.type: com.company.datamapper.domain.EmpUUID
spring.json.value.default.type: com.fasterxml.jackson.databind.JsonNode
commit.interval.ms: 1000
spring.cloud.stream.kafka.binder.headerMapperBeanName:
customKafkaHeaderMapper
但是,此配置无法正常工作,并且出现以下错误。
Caused by: java.lang.IllegalArgumentException: The class 'com.fasterxml.jackson.databind.node.ObjectNode' is not in the trusted packages: [java.util, java.lang, com.fasterxml.jackson.databind]. If you believe this class is safe to deserialize, please provide its name. If the serialization is only done by a trusted source, you can also enable trust all (*).
at org.springframework.kafka.support.converter.DefaultJackson2JavaTypeMapper.getClassIdType(DefaultJackson2JavaTypeMapper.java:125) ~[spring-kafka-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.kafka.support.converter.DefaultJackson2JavaTypeMapper.toJavaType(DefaultJackson2JavaTypeMapper.java:99) ~[spring-kafka-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.springframework.kafka.support.serializer.JsonDeserializer.deserialize(JsonDeserializer.java:425) ~[spring-kafka-2.3.7.RELEASE.jar:2.3.7.RELEASE]
at org.apache.kafka.streams.processor.internals.SourceNode.deserializeValue(SourceNode.java:60) ~[kafka-streams-2.3.1.jar:?]
at org.apache.kafka.streams.processor.internals.RecordDeserializer.deserialize(RecordDeserializer.java:66) ~[kafka-streams-2.3.1.jar:?]
我有2个查询
答案 0 :(得分:0)
问题出在解串器中,而不是标题映射器中。
at org.springframework.kafka.support.serializer.JsonDeserializer.deserialize(JsonDeserializer.java:425)
spring.json.value.default.type: com.fasterxml.jackson.databind.JsonNode
我们将自动“信任”默认类型的软件包,但不信任子软件包。
您需要将...node
软件包添加到
spring.json.trusted.packages
在活页夹配置中。
我们可能应该支持信任子程序包;随时打开an issue on GitHub against spring-kafka。