带有equals的Spring @RequestMapping标头 [英] Spring @RequestMapping header with equals
问题描述
我想用Spring @RequestMapping和header属性来检测值 application / json; version = 1的
。该计划将为版本2映射另一个方法,该版本的值将为 Accept
头。* application / json; version = 2。*
。
春天似乎忽略了版本值。我猜它是把等号看作是另一个标题属性。
有没有办法解决这个问题?
附注:
- 我无法更新Spring版本以支持
消耗
属性 - 我无法更改请求标头的格式
- I can't update the Spring version to support the
consumes
attribute - I can't change the format the request header will come in
ol>
似乎spring requestmapping会忽略媒体类型参数。
您可以通过手动将请求路由到您的首选终端来解决此问题。
@RequestMapping(value =/ ),header =Accept = application / json)
@ResponseBody
字符串请求(@RequestHeader HttpHeaders标题){
for(MediaType mediaType:headers.getAccept()){$ b $如果(mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)){
if(mediaType.getParameter(version)。startsWith(1。)){
return v1();
} else if(mediaType.getParameter(version)。startsWith(2。)){
return v2();
}
}
}
返回错误;
}
I want to use Springs @RequestMapping with the header attribute to detect an Accept
header with the value application/json;version=1.*
. The plan is to have another method mapped similarly for version 2 which will have the value application/json;version=2.*
.
Spring seems to be ignoring the version value. I'm guessing it's treating the equals sign as another header attribute.
Is there a way around this?
Side notes:
It seems that the spring requestmapping ignores media type parameters. You can work around this by manually routing the request to your preferred endpoint.
@RequestMapping(value = "/", headers = "Accept=application/json")
@ResponseBody
String request(@RequestHeader HttpHeaders headers){
for(MediaType mediaType : headers.getAccept()){
if(mediaType.isCompatibleWith(MediaType.APPLICATION_JSON)){
if(mediaType.getParameter("version").startsWith("1.")){
return v1();
}else if(mediaType.getParameter("version").startsWith("2.")){
return v2();
}
}
}
return "error";
}
这篇关于带有equals的Spring @RequestMapping标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!