Java 如何配置spring以忽略无效的Accept标头?
我正在使用spring构建我的web应用程序 在我的自定义Java 如何配置spring以忽略无效的Accept标头?,java,spring,http,spring-mvc,http-accept-header,Java,Spring,Http,Spring Mvc,Http Accept Header,我正在使用spring构建我的web应用程序 在我的自定义WebMvcConfigurationSupport类中,我设置了基本ContentNegotiationConfigurer,如下所示: @Override public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) { configurer .favorPathExtension(false
WebMvcConfigurationSupport
类中,我设置了基本ContentNegotiationConfigurer
,如下所示:
@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
configurer
.favorPathExtension(false)
.favorParameter(true)
.parameterName("mediaType")
.ignoreAcceptHeader(false)
.useJaf(false)
.defaultContentType(MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
}
我无法将ignoreAcceptHeader
设置为true
,因为我的一些客户依赖此头进行响应
但是,当我尝试使用无效的Accept
标题(如Accept::*/*
(请注意额外的冒号)访问API时,spring会重定向到错误页面/error
,并显示以下日志:
12:18:14.49848443[6061][qtp1184831653-73]调试o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver
从处理程序解析异常[public MyController.myAction()抛出java.io.IOException]:org.springframework.web.HttpMediaTypeNotAcceptableException:
无法分析接受头[:application/json,*/*]:无效的mime类型:application/json:无效的令牌字符“:”在令牌:application中
我能改变这种行为吗?我想完全忽略
接受标题,而不是跳转到错误页面。这可能吗?使用筛选器拦截具有错误标头的请求,并将其包装以替换(或删除)错误标头
在本例中,将getHeader()
方法更改为
public String getHeader(String name) {
if ("accept".equals(name)) {
return null; //or any valid value
}
String header = super.getHeader(name);
return (header != null) ? header : super.getParameter(name);
}