使用spring-boot-stream
读取队列/ Exchange rabbitMQ时,它将返回以下Exception
:
Unable to deserialize [br.com.foo.Bar] using the contentType [application/x-java-object;type=br.com.foo.Bar] br.com.foo.Bar; nested exception is java.lang.ClassNotFoundException
我了解在我的应用程序的bar
中找不到类package
,但是我想通过忽略contentType来接收此消息,因此我可以在任何满足以下条件的类中接收该消息:实现属性。
是否可以跳过或绕过此header?
@EnableBinding(CustomExchange.class)
public class RabbitService {
@StreamListener(target = CustomExchange.INPUT)
public void recievedMessage(Message<br.etc.Bar> msg) {
try {
Bar bar = msg.getPayload();
//faz algo
} catch (Exception e) {
// faz algo
}
}
}
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
答案 0 :(得分:1)
可以升级到2.0.1吗?默认序列化技术使用JSON而不是Kryo。
在1.3.x中,您可以通过将出站绑定的contentType设置为application/json
来更改为JSON。
编辑
这对我来说很好...
@SpringBootApplication
@EnableBinding(Processor.class)
public class So52938080Application {
public static void main(String[] args) {
SpringApplication.run(So52938080Application.class, args);
}
@StreamListener(Processor.INPUT)
public void listen(Message<Bar> message) {
System.out.println(message);
}
@Bean
public ApplicationRunner runner(MessageChannel output) {
return args -> {
output.send(new GenericMessage<>(new Foo("baz")));
};
}
public static class Foo {
private String bar;
public Foo() {
super();
}
public Foo(String bar) {
this.bar = bar;
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
public static class Bar {
private String bar;
public Bar() {
super();
}
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
@Override
public String toString() {
return "Bar [bar=" + this.bar + "]";
}
}
}
和
spring.cloud.stream.bindings.output.destination=foo
spring.cloud.stream.bindings.output.content-type=application/json
spring.cloud.stream.bindings.input.destination=foo
spring.cloud.stream.bindings.input.content-type=application/json
spring.cloud.stream.bindings.input.group=foo
和
GenericMessage [有效载荷=栏[栏=巴兹],标头= {amqp_receivedDeliveryMode = PERSISTENT,amqp_receivedRoutingKey = foo,amqp_receivedExchange = foo,amqp_deliveryTag = 1,amqp_consumerQueue = foo.foo,amqp_redelivered45 = 6,-4889 -b7bc-16c9ade11941,amqp_consumerTag = amq.ctag-GGu9q7nO6KTpZ-NRwTe9xA,contentType = application / json; charset = UTF-8,timestamp = 1540247495149}]