【问题标题】:Spring Web MVC :: GET method :: RequestParam encodingSpring Web MVC :: GET 方法 :: RequestParam 编码
【发布时间】:2016-09-13 13:03:43
【问题描述】:

我有一个 REST 入口点:

@RequestMapping(value = "user" method = RequestMethod.GET)
public List<User> getUsers( @RequestParam(value = "name") String name )

我正在使用 AngularJS 向 /rest/user?name=Иван 发送,请求在 rest/user?name=%D0%98%D0%B2%D0%B0%D0%BD 中使用 urlencode 编码。

但该方法在ISO_8859_1 字符集中接收name,而不是像预期的那样以UTF-8 接收。 如果我写

new String(request.getName().getBytes( StandardCharsets.ISO_8859_1 ), StandardCharsets.UTF_8)

它变得很好。但这不是正确的方法:)

我做错了什么以及如何解决?

===

P。

在我的 web.xml 中,我有一个为 UTF-8 编码配置的 CharacterEncodingFilter

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>

【问题讨论】:

    标签: java angularjs spring rest


    【解决方案1】:

    您是否使用 gradle 构建您的项目?构建时尝试标志-Dfile.encoding=utf-8,默认编码是系统编码

    【讨论】:

    • 我使用 maven,并且我在 maven-compiler-plugin 属性中有 &lt;encoding&gt;UTF-8&lt;/encoding&gt;。此外,我所有的文件都是 UTF-8 格式。
    • 好吧,然后尝试将&lt;init-param&gt; &lt;param-name&gt;forceEncoding&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; 添加到您的过滤器设置中
    • 但我不需要 forceEncoding - 它会设置响应编码,但问题是关于请求。查看来源github.com/spring-projects/spring-framework/blob/master/…
    • 如果你不添加 forceEncoding,这个过滤器什么也不做
    • 它没有 - 它将请求编码设置为提供(在我的情况下为 UTF-8)。查看源代码github.com/spring-projects/spring-framework/blob/master/… 此行在调试器下运行。
    猜你喜欢
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 2013-09-09
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多