简体   繁体   English

春天@RequestParam Unicode无法正确获取

[英]spring @RequestParam Unicode not getting correctly

I am using Spring to create RESTful service in java. 我正在使用Spring在Java中创建RESTful服务。 I have controller defined below: 我在下面定义了控制器:

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);
    }  

}

Now i have call it from different clients to accurate if problem on client or service. 现在,我已经从不同的客户那里调用它,以准确确定客户或服务上的问题。 Last i have tried on: chrome-extension: POSTMAN. 最后我尝试过:chrome-extension:邮递员。 i am executing this method with POST option and my request query string is: https://xx_mydomain_xx.com/fake/favorites/save/sessionId=cd6c5d88-771e-4831-a30a-c414fafa2803&comment =сяма&serviceId=157&fields=blablablafieds 我正在使用POST选项执行此方法,并且我的请求查询字符串是: https ://xx_mydomain_xx.com/fake/favorites/save/sessionId=cd6c5d88-771e-4831-a30a-c414fafa2803&comment =сяма&serviceId = 157&fields = blablablafieds

I have logged sent Request Parameters. 我已经记录了发送的请求参数。 And on my logs i see that russian characters is not posting correctly. 在我的日志中,我看到俄语字符未正确发布。 For example i have posted Сяма but in my logs i have found Ñ?Ñ?ма 例如,我发布了Сяма,但在我的日志中发现了Ñ?Ñ?ма

I have added filters to web.xml file: 我已将过滤器添加到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>

But it did not help me 但这没有帮助我

What can you advise to me? 你能给我什么建议?

Dont you need to URL encode these parameters in your test request? 您是否不需要在测试请求中对这些参数进行URL编码? Else they will break when decoded in your request parameter mapping step, just like you described 否则,当您在请求参数映射步骤中对其进行解码时,它们将中断,就像您描述的那样

I am not sure if my answer will be helpful for other servlet containers but on Tomcat you need also specify URIEncoding attribute of each Connector element within conf/server.xml . 我不确定我的回答是否对其他servlet容器有用,但是在Tomcat上,您还需要在conf/server.xml为每个Connector元素指定URIEncoding属性。

<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 />

For more information look at Tomcat documentation 有关更多信息,请参阅Tomcat文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM