gpt4 book ai didi

Spring @RequestMapping 消耗字符集?

转载 作者:行者123 更新时间:2023-12-01 07:41:46 29 4
gpt4 key购买 nike

我正在尝试使用 @RequestMappingconsumes -元素。阅读API-document它适用于 Content-Type - 请求头。但是,使用

@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=UTF-8", value = "/test")
public void test() {
:
}

或者
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=ISO-8859-1", value = "/test")
public void test() {
:
}

没什么区别。请求中的 header 看起来像
Content-Type: application/x-www-form-urlencoded;charset=UTF-8

或者
Content-Type: application/x-www-form-urlencoded
test()将在所有四个可能的星座中调用。

然而,这向我证明了 Spring 看到并尝试使用 charset -部分,如果我指定
@RequestMapping(consumes = "application/x-www-form-urlencoded;charset=UTF-x8", value = "/test")
public void test() {
:
}

我在网络应用程序的启动 (!) 期间遇到异常:
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

请注意 produces 上的文档-element 也没有提到 charset 的使用,但根据谷歌的说法,有些人使用它。

关于这里发生的事情或我做错了什么的任何线索?

顺便说一句,这是 Spring 3.1.1.RELEASE。

最佳答案

我想你已经回答了你的问题,所以从代码的角度来看,这更多的是确认为什么charset解析映射时不考虑。

在深入研究 Spring 代码时,罪魁祸首似乎是 MediaType#includes() .方法

更多的挖掘表明 RequestMappingInfo RequestMapping 相关联而创建方法的注释。此 RequestMappingInfo专卖店系列AbstractRequestCondition对象,其中之一是 ConsumesRequestCondition 其中包含 MediaType consumes 中定义注释的一部分(即 application/x-www-form-urlencoded;charset=UTF-8 )。

稍后提出请求时,此 ConsumesRequestCondition有一个 inner ConsumeMediaTypeExpression class with a matchMediaType() method提取 MediaTypeHttpServletRequest并根据它自己的情况进行检查 MediaType看看它是否包含在内。

如果你看 MediaType#includes()实现(第 426 到 428 行),当 type 时返回 true (即 application )和 subtype (即 x-www-form-urlencoded )是相等的,完全无视 parameters在这种情况下映射
持有残余"charset","UTF-8"组合。

深挖produces track 似乎显示了类似的结果,但在本例中是 MediaType#isCompatibleWith() 涉及的方法,再次,它只到达typesubtype如果他们相等。

如果您在 Google 上找到了 produces 的证据为字符集请求映射工作,我对此表示怀疑(除非他们更改了核心 Spring 内容)

至于为什么这样设计,那是另一个问题:)

关于Spring @RequestMapping 消耗字符集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12798384/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com