-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
Priority header causes binding exception after upgrade to Spring Framework 6.2.0 #34039
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Thanks for the report. There is a way, but it is not very convenient. Headers and path vars are added from To make this easier, we can add a @ControllerAdvice
public class MyControllerAdvice {
@InitBinder
public void initBinder(ExtendedServletRequestDataBinder binder) {
binder.addHeaderPredicate(header -> ... );
}
} The "Priority" header seems to be RFC-defined and common to both this and the report in #33961. We can exclude it by default as it is likely to cause more surprise. |
Make it public and move it down to the annotations package alongside InitBinderBindingContext. This is mirrors the hierarchy in Spring MVC with the ExtendedServletRequestDataBinder. The change will allow customization of the header names to include/exclude in data binding. See gh-34039
@rstoyanchev public class XRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter {
private Set<String> headers = new HashSet<>();
public void setHeaders (Set<String> headers) {
this.headers = headers;
}
@Override
protected InitBinderDataBinderFactory createDataBinderFactory (List<InvocableHandlerMethod> a) {
return new XServletRequestDataBinderFactory (a , getWebBindingInitializer() , this.headers);
}
public static final class XServletRequestDataBinderFactory extends InitBinderDataBinderFactory {
private final Set<String> headers;
public XServletRequestDataBinderFactory (List<InvocableHandlerMethod> a, WebBindingInitializer b, Set<String> c) {
super(a, b); this.headers = c;
}
@Override
protected ServletRequestDataBinder createBinderInstance(Object a, String b, NativeWebRequest c) throws Exception {
var r = new ExtendedServletRequestDataBinder(a , b); r.setHeaderPredicate(h -> headers.contains(h)); return r;
}
}
} However, we encountered issues when attempting to register this class. We are using traditional Spring MVC XML configuration to manage dependencies. for example <mvc:annotation-driven conversion-service="web.admin.conversion.service">
<mvc:argument-resolvers>
<bean class="xx.xx.XXArgumentResolver"/>
</mvc:argument-resolvers>
<mvc:path-matching suffix-pattern = "false" path-matcher = "web.admin.path.matcher"/>
<mvc:message-converters>
<bean class="xx.glossary.spring.converter.OctetHttpMessageConverter">
</bean>
<bean class="xx.glossary.spring.converter.JsonHttpMessageConverter" >
<property name="marshaller" ref="web.admin.json.marshaller" />
</bean>
</mvc:message-converters>
<mvc:async-support default-timeout="90000" task-executor="web.admin.async.executor">
<mvc:callable-interceptors>
<bean id="web.admin.async.aspect" class="xx.aspect.AsyncInterceptor"/>
</mvc:callable-interceptors>
</mvc:async-support>
</mvc:annotation-driven> Is there a simple way to register |
@leonchen83 unfortunately no, there is no way to register a custom |
Overview
After upgrading to Spring Framework 6.2, I encountered an issue where request headers such as
priority
are automatically bound to the parameters in my controller. This behavior wasn't present in earlier versions (e.g., 6.1). My intention is to treat missing parameters (likepriority
) as default values (e.g., 0), but now the framework seems to bind unexpected header values likepriority: u=1, i
, which leads to unexpected behavior.I would like to know if there’s a way to disable automatic header binding or configure Spring to ignore specific headers like
priority
when processing requests.Example
When we register this
ShortConverter
and submit a form request like this:type=1&status=1&companyId=1&name=tt&abbreviation=tt&tradeTypes=7&sides=3&orderTypes=85
Our intention is that when
priority
is not set, it should be treated as0
. This worked correctly in Spring Framework versions prior to 6.2. However, after upgrading to 6.2, it no longer works as expected.For example, in Chrome, the complete request looks like this:
In this request, the
priority
header is present but contains unexpected values likeu=1, i
. After the upgrade, this causes issues with handling the request in Spring Framework 6.2.related issue 32676
Question
Is there a way to prevent binding request headers in Spring Framework?
The text was updated successfully, but these errors were encountered: