-
Notifications
You must be signed in to change notification settings - Fork 626
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
RabbitListener does not include inferrer value when injecting header object #1441
Comments
Thanks for reporting; reproduced: @SpringBootApplication
public class Rgh1441Application {
public static void main(String[] args) {
SpringApplication.run(Rgh1441Application.class, args);
}
@RabbitListener(queues = "foo")
public void listen1(Foo foo, @Headers Map<String, Object> headers) {
System.out.println("1: " + foo);
}
@RabbitListener(queues = "bar")
public void listen2(Foo foo, MessageHeaders headers) {
System.out.println("2: " + foo);
}
@Bean
ApplicationRunner runner(RabbitTemplate template, AbstractRabbitListenerContainerFactory<?> factory) {
factory.setMessageConverter(converter());
return args -> {
template.convertAndSend("foo", "{\"bar\":\"baz\"}", msg -> {
msg.getMessageProperties().setContentType("application/json");
return msg;
});
template.convertAndSend("bar", "{\"bar\":\"baz\"}", msg -> {
msg.getMessageProperties().setContentType("application/json");
return msg;
});
};
}
Jackson2JsonMessageConverter converter() {
return new Jackson2JsonMessageConverter();
}
public static class Foo {
private String bar;
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
@Override
public String toString() {
return "Foo [bar=" + this.bar + "]";
}
}
}
|
The issue is in the code that determines whether the parameter is a The solution is to also add @RabbitListener(queues = "foo")
public void listen1(Foo foo, @Headers Map<String, Object> headers) {
System.out.println("1: " + foo);
}
@RabbitListener(queues = "bar")
public void listen2(@Payload Foo foo, @Headers MessageHeaders headers) {
System.out.println("2: " + foo);
}
|
I see but then the documentation is misleading. `...Annotated methods are allowed to have flexible signatures similar to what MessageMapping provides, that is @Header-annotated method arguments to extract a specific header value, including standard AMQP headers defined by AmqpHeaders @Headers-annotated argument that must also be assignable to java.util.Map for getting access to all headers. MessageHeaders arguments for getting access to all headers. Anyway thanks for your efforts! |
Right; I am going to fix it; I just wanted to tell you how to resolve it without any framework changes. |
Resolves spring-projects#1441 Previously, `MessageHeaders` had to be annotated with `@Headers` so that it was ignored during payload parameter resolution; otherwise it caused ambiguity. Ignore `MessageHeaders` even when not so annotated. Also fix some tests that were checking the same topic and count down latch so were unconditionally passing. Change one of those tests to verify the fix. **cherry-pick to 2.4.x, 2.3.x**
Resolves spring-projects#1441 Previously, `MessageHeaders` had to be annotated with `@Headers` so that it was ignored during payload parameter resolution; otherwise it caused ambiguity. Ignore `MessageHeaders` even when not so annotated. Also fix some tests that were checking the same topic and count down latch so were unconditionally passing. Change one of those tests to verify the fix. **cherry-pick to 2.4.x, 2.3.x**
Resolves #1441 Previously, `MessageHeaders` had to be annotated with `@Headers` so that it was ignored during payload parameter resolution; otherwise it caused ambiguity. Ignore `MessageHeaders` even when not so annotated. Also fix some tests that were checking the same topic and count down latch so were unconditionally passing. Change one of those tests to verify the fix. **cherry-pick to 2.4.x, 2.3.x**
Resolves #1441 Previously, `MessageHeaders` had to be annotated with `@Headers` so that it was ignored during payload parameter resolution; otherwise it caused ambiguity. Ignore `MessageHeaders` even when not so annotated. Also fix some tests that were checking the same topic and count down latch so were unconditionally passing. Change one of those tests to verify the fix. **cherry-pick to 2.4.x, 2.3.x**
Resolves #1441 Previously, `MessageHeaders` had to be annotated with `@Headers` so that it was ignored during payload parameter resolution; otherwise it caused ambiguity. Ignore `MessageHeaders` even when not so annotated. Also fix some tests that were checking the same topic and count down latch so were unconditionally passing. Change one of those tests to verify the fix. **cherry-pick to 2.4.x, 2.3.x**
Version:
Spring Boot 2.6 - spring starter
############
Description:
The documentation of the RabbitListener annotation suggests multiple ways on how to retrieve the message headers. If you use MessageHeaders and inject it as an argument for getting access to all headers, then the inferrer header (which in my understanding contains the consumer model) is not provided with a value anymore and message converter implementations like the Jackson2JsonMessageConverter are then using the TYPE_ID property and convert it into the given domain model. That property is provided by the producer and usually contains the fully qualified package name, so the converter on the consumer side looks for the domain model of the producer. If the consumer has its model not in the exact same package structure, it throws an ClassNotFound exception. This is confusing because if you cut the MessageHeader object and instead inject a simple Map<String, Object> and annotate that with @headers (also suggested in the docs) then it works again and the inferrer value is provided. So my guess is this behaviour is not intended.
Steps to reproduce the behavior:
Actual behavior
ClassnotFound exception is thrown and converter cannot convert into inferrer type.
Expected behavior
Inferrer type is provided and converts successfully into consumers model.
Sample
A short sample will be provided soon.
The text was updated successfully, but these errors were encountered: