Requestparam获取的值不正确

时间:2016-08-23 01:15:17

标签: java spring utf-8

查询参数网址: AspectValues?aspectName = US + Shoe + Size +(女性%27s)

当我使用时:

@RequestMapping(value = "AspectValues", method = RequestMethod.GET)
@ResponseBody
public Map<String, String> getAspectValues(@RequestParam String aspectName)

aspectName填充为"US Shoe Size (Women s)"而不是"US Shoe Size (Women's)"

如果你们中的任何人在编码百分比时能让我知道,它会给出任何值作为空格以及这种行为的原因,那将会很有帮助。

2 个答案:

答案 0 :(得分:0)

添加charecter编码过滤器

<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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

或bean

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    registrationBean.setFilter(characterEncodingFilter);
    return registrationBean;
}

答案 1 :(得分:-1)

这个问题并不是针对Spring的。相反,它更多地涉及Java如何处理uri。您可以查看java.net.uri来解决此问题。

这个问题已经很好地回答了post