spring @RequestParam Unicode无法正确显示

时间:2014-12-10 13:47:52

标签: java spring rest

我使用Spring在java中创建RESTful服务。 我在下面定义了控制器:

package com.pro.controller;    

@RestController
@RequestMapping("/favorites")
public class FavoritesController extends SkeletonController {

    protected static Logger log = Logger.getLogger("FavoritesController");

    @Autowired
    private FavoritesService service;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public StandardResult savePaymentList(
            @RequestParam(value = "sessionId", required = true) String sessionId,
            @RequestParam(value = "comment", required = true) String comment,
            @RequestParam(value = "serviceId", required = true) int serviceId,
            @RequestParam(value = "fields", required = true) String fields) throws Exception {

        log.info("sessionId:" + sessionId + ", serviceId:" + serviceId + ", fields:" + fields + ", comment:" + comment);

        SavePaymentListRequest request = new SavePaymentListRequest();
        request.setLang("en");
        request.setSid(sessionId);
        request.setSessionRequestType(SessionRequestType.MOBILE);


        request.setFields(arrOfFields);
        return service.savePaymentList(request);
    }  

}

现在我已经从不同的客户端调用它来准确,如果客户端或服务有问题。 最后我试过:chrome-extension:POSTMAN。 我正在使用POST选项执行此方法,我的请求查询字符串是: https://xx_mydomain_xx.com/fake/favorites/save/sessionId=cd6c5d88-771e-4831-a30a-c414fafa2803&comment =сяма&安培;服务Id = 157安培;字段= blablablafieds

我已记录已发送的请求参数。在我的日志中,我看到俄语字符未正确发布。例如,我发布了Сяма,但在我的日志中,我发现Ñ?Ñ?ма

我在web.xml文件中添加了过滤器:

 <filter>
    <filter-name>encoding-filter</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>
    <filter-mapping>
        <filter-name>encoding-filter</filter-name>
        <url-pattern>/*</url-pattern>
 </filter-mapping>

但它没有帮助我

你能告诉我什么?

2 个答案:

答案 0 :(得分:0)

您是否需要在测试请求中对这些参数进行URL编码?否则,它们会在您的请求参数映射步骤中解密,就像您描述的那样

答案 1 :(得分:0)

我不确定我的答案是否对其他servlet容器有帮助,但在Tomcat上,您还需要在URIEncoding中指定每个Connector元素的conf/server.xml属性。

<Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" OTHER_ATTRIBUTES />

<Connector SSLEnabled="true" port="8443" scheme="https" secure="true" URIEncoding="UTF-8" OTHER_ATTRIBUTES />

有关详细信息,请查看Tomcat documentation