Skip to content
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

HeaderMethodArgumentResolver doesn't resolve Optional.empty() for non-existing header [SPR-15151] #19717

spring-issuemaster opened this issue Jan 17, 2017 · 1 comment


Copy link

commented Jan 17, 2017

Artem Bilan opened SPR-15151 and commented

The test-case to reproduce:

public void resolveNullHeaderToOptional() throws Exception {
	class Foo {

		public void optionalHeader(@Header("foo") Optional<String> foo) {



	GenericApplicationContext cxt = new GenericApplicationContext();

	HeaderMethodArgumentResolver resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(),

	Method method = ReflectionUtils.findMethod(Foo.class, "optionalHeader", (Class<?>[]) null);
	MethodParameter optionalParameter = new SynthesizingMethodParameter(method, 0);

	Message<String> message = MessageBuilder.withPayload("foo")
//				.setHeader("foo", "bar")
	Object result = resolver.resolveArgument(optionalParameter, message);
	assertEquals(Optional.empty(), result);

The "guilty" code is here in the AbstractNamedValueMethodArgumentResolver:

if (!ClassUtils.isAssignableValue(parameter.getParameterType(), arg)) {
	arg = this.conversionService.convert(arg, TypeDescriptor.forObject(arg), new TypeDescriptor(parameter));

Meanwhile org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver delegates to the ConversionService independently of the types compatibility:

try {
	arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
catch (ConversionNotSupportedException ex) {

The here is like currently Spring Integration relies on the SpEL method invocation and we can't properly move to the InvocableHandlerMethod foundation, because of missing conversions.

I guess I can come up as a workaround with something like OptionalHeaderMethodArgumentResolver implementation, but would be great to have a fix in the Core as well.


Affects: 4.3.5


This comment has been minimized.

Copy link
Collaborator Author

commented Jan 17, 2017

Juergen Hoeller commented

I've revised the condition to consider nested parameters as well. So we're still not unconditionally triggering conversion but at least we're always triggering it for Optional now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.