我正在尝试使用希伯来语中的查询字符串参数发送GET请求.
当控制器获得请求时,参数是乱码.
我添加了“org.springframework.web.filter.CharacterEncodingFilter”,但它没有改变一件事.
请告知如何解决它.
更新:这是请求.
GET /myapp/specialties?query=%D7%92%D7%99%D7%A0%D7%A0%D7%A0%D7%A0 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: *
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Content-Type: application/json;charsert=utf-8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,he;q=0.6
Query String Parametersview sourceview URL encoded
query:גיננננ
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Mon, 03 Mar 2014 20:45:17 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
web.xml中
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
med.rec
contextConfigLocation
classpath:spring/application-config.xml
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/mvc-config.xml
1
dispatcherServlet
/
-Roy
解决方法:
Overrides the name of the character encoding used in the body of this
request. This method must be called prior to reading request
parameters or reading input using getReader(). Otherwise, it has no
effect.
这对你没有好处,因为你没有从正文中获取参数,而是从查询字符串中获取参数.
但是,如果您使用的是Tomcat,那么您很幸运. Tomcat有一个特殊的Connector属性,当设置它(默认情况下未设置)时,将对查询字符串使用相同的字符编码.
该属性为useBodyEncodingForURI.如果打开Tomcat servet.xml文件,您将找到一个类似的元素(没有属性)
redirectPort="8443" useBodyEncodingForURI="true">
添加属性,它将按预期工作.在这种情况下,请确保为相应的Connector,HTTP设置它.
其他Servlet容器可能有一些类似的配置.
标签:java,servlets,utf-8,character-encoding,servlet-filters
来源: https://codeday.me/bug/20190528/1174724.html