[英]Spring @RequestMapping consumes charset?
I'm trying to use @RequestMapping
with the consumes
-element. 我正在尝试使用带consumes
元素的@RequestMapping
。 Reading the API-document it works on the Content-Type
-header of the request. 阅读API文档,它适用于请求的Content-Type
-header。 However, using 但是,使用
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=UTF-8", value = "/test")
public void test() {
:
}
or 要么
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=ISO-8859-1", value = "/test")
public void test() {
:
}
doesn't make a difference. 并没有什么区别。 The header in the request can look like 请求中的标头可能看起来像
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
or 要么
Content-Type: application/x-www-form-urlencoded
test()
will be called in all four possible constellations. test()
将在所有四个可能的星座中被调用。
However, and this is proof to me that Spring sees and tries to use the charset
-part, if I specify 但是,这可以证明Spring看到并尝试使用charset
-part,如果我指定的话
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=UTF-x8", value = "/test")
public void test() {
:
}
I get an exception during startup (!) of the web-app: 我在web-app的启动(!)期间遇到异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed;
nested exception is java.nio.charset.UnsupportedCharsetException: UTF-x8
Note that the documentation on the produces
-element also doesn't mention the use of charset
, but according to Google some use it. 请注意,在文档produces
-元素也并没有提到使用charset
,但根据谷歌的一些使用它。
Any clues to what's happening here or what I'm doing wrong? 这里发生了什么或者我做错了什么的线索?
BTW, this is Spring 3.1.1.RELEASE. 顺便说一句,这是Spring 3.1.1.RELEASE。
I think you have already answered your question, so this is more of a confirmation, from a code point of view, as to why the charset
is not taken into account when resolving mappings. 我想你已经回答了你的问题,所以从代码的角度来看,这更像是一个确认,为什么在解析映射时不考虑charset
。
When digging into Spring code, the culprit seems to be the MediaType#includes()
. 在深入研究Spring代码时,罪魁祸首似乎是MediaType#includes()
。 method 方法
More digging reveals that a RequestMappingInfo
is created in association to the RequestMapping
annotation of the method. 更多挖掘揭示了RequestMappingInfo
是与方法的RequestMapping
注释相关联地创建的。 This RequestMappingInfo
stores a series of AbstractRequestCondition
objects, one of them being the ConsumesRequestCondition
which holds the MediaType
defined in the consumes
part of the annotation (ie application/x-www-form-urlencoded;charset=UTF-8
). 此RequestMappingInfo
存储一系列AbstractRequestCondition
对象,其中一个是ConsumesRequestCondition
,它保存在注释的consumes
部分中定义的MediaType
(即application/x-www-form-urlencoded;charset=UTF-8
)。
Later when a request is made, this ConsumesRequestCondition
has an inner ConsumeMediaTypeExpression
class with a matchMediaType()
method that extracts the MediaType
of the HttpServletRequest
and checks it against it's own MediaType
to see if it's included. 后,当一个请求时,这个ConsumesRequestCondition
具有内ConsumeMediaTypeExpression
与类matchMediaType()
方法 ,其提取所述MediaType
的的HttpServletRequest
,并检查它针对它自己MediaType
看它是否包括在内。
If you look at the MediaType#includes()
implementation (Lines 426 to 428), it returns true when type
(ie application
) and subtype
(ie x-www-form-urlencoded
) are equal, completely disregarding the parameters
Map which in this case holds the remnant "charset","UTF-8"
combination. 如果你看一下MediaType#includes()
实现(第426行到第428行),当type
(即application
)和subtype
(即x-www-form-urlencoded
)相等时,它返回true,完全忽略parameters
Map,在这里案件持有残余的"charset","UTF-8"
组合。
Digging into the produces
track seems to show similar results, but in this case it's the MediaType#isCompatibleWith()
method involved, and again, it reaches only to type
and subtype
if they are equal. 挖掘到produces
轨迹似乎显示出类似的结果,但在这种情况下,它涉及MediaType#isCompatibleWith()
方法,并且如果它们相等,它只会到达type
和subtype
。
If you found evidence on Google of the produces
working for charset request mapping, I would doubt it (unless they changed core Spring stuff) 如果你在Google上发现了为charset请求映射工作的produces
证据,我会怀疑它(除非他们改变了核心Spring的东西)
As to why it was designed this way, well that's another question :) 至于它为什么这样设计,这是另一个问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.