sjn*_*ngm 3 spring httprequest character-encoding
我试图用@RequestMapping
同consumes
-元素.阅读API文档,它可以在Content-Type
请求的头文件上工作.但是,使用
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=UTF-8", value = "/test")
public void test() {
:
}
Run Code Online (Sandbox Code Playgroud)
要么
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=ISO-8859-1", value = "/test")
public void test() {
:
}
Run Code Online (Sandbox Code Playgroud)
并没有什么区别.请求中的标头可能看起来像
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Run Code Online (Sandbox Code Playgroud)
要么
Content-Type: application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
test()
将在所有四个可能的星座中被召唤.
但是,charset
如果我指定的话,这可以证明Spring看到并尝试使用-part
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=UTF-x8", value = "/test")
public void test() {
:
}
Run Code Online (Sandbox Code Playgroud)
我在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
Run Code Online (Sandbox Code Playgroud)
请注意,关于produces
-element 的文档也没有提到使用charset
,但根据谷歌的一些使用它.
这里发生了什么或者我做错了什么的线索?
顺便说一下,这是Spring 3.1.1.RELEASE.
bet*_*ejo 12
我想你已经回答了你的问题,所以从代码的角度来看,这更像是一个确认,为什么charset
在解析映射时不考虑这个问题.
在深入研究Spring代码时,罪魁祸首似乎就是这样MediaType#includes()
.方法
更多挖掘揭示了a 与方法RequestMappingInfo
的RequestMapping
注释相关联地创建.这RequestMappingInfo
将存储一系列AbstractRequestCondition
对象,其中一个对象ConsumesRequestCondition
包含注释(即)部分中MediaType
定义的对象.consumes
application/x-www-form-urlencoded;charset=UTF-8
后来,当一个请求时,这ConsumesRequestCondition
有一个内部ConsumeMediaTypeExpression
类与matchMediaType()
方法是提取MediaType
的HttpServletRequest
和检查它反对自己MediaType
,看看它是否包含在内.
如果你看一下MediaType#includes()
实现(第426行到第428行),它会在type
(ie application
)和subtype
(ie x-www-form-urlencoded
)相等时返回true ,完全忽略parameters
Map,在这种情况下,Map保持剩余"charset","UTF-8"
组合.
挖掘produces
轨道似乎显示出类似的结果,但在这种情况下,它是所MediaType#isCompatibleWith()
涉及的方法,并且再次,它仅达到type
并且subtype
如果它们相等.
如果你在谷歌上发现了produces
为charset请求映射工作的证据,我会怀疑它(除非他们改变了核心Spring的东西)
至于它为什么这样设计,这是另一个问题:)
归档时间: |
|
查看次数: |
15816 次 |
最近记录: |