如何在 spring api 中使用元音变音
how to use umlaut with spring api
我在向 Spring API 发送变音符号时遇到问题。我想 post 关注 JSON:
{
"username": "testümlaut",
"firstName": "test",
"lastName": "Test"
}
为此,我有以下方法开始:
@RequestMapping(value="/User", method=RequestMethod.POST,
produces={"application/json ; charset=utf-8"}
)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody User postUser(@RequestBody User user) {
User user = userDao.addUser(user);
return user;
}
如你所见,我有一行:
produces={"application/json ; charset=utf-8"}
但这并没有帮助。我总是遇到异常(0xfc 是 ü):
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Invalid UTF-8 start byte 0xfc
at [Source: java.io.PushbackInputStream@721e5ed1; line: 2, column: 19] (through reference chain: de.escosautomation.restserver.model.user.UserClone["username"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xfc
at [Source: java.io.PushbackInputStream@721e5ed1; line: 2, column: 19] (through reference chain: de.escosautomation.restserver.model.user.UserClone["username"])
我还可以添加什么来让它发挥作用?
谢谢。
您可以在此处检查几个选项:
在您的应用程序服务器上编码。例如,在 Tomcat 上,在所有连接器上确保将 URIEncoding 设置为 UTF-8。例如:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" URIEncoding="UTF-8"/>
- 当您发送 JSON 时,您需要 消耗和
产生设置为
MediaType.APPLICATION_JSON_UTF8_VALUE
(application/json;字符集=UTF-8)。例如:
@RequestMapping(value="/User", method=RequestMethod.POST,
consumes=MediaType.APPLICATION_JSON_UTF8_VALUE,
produces=MediaType.APPLICATION_JSON_UTF8_VALUE
)
在web.xml:
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其实我接受尤里·尤尼科夫的回答。
但是如果有人在使用 SOAPui,请不要忘记在那里设置请求的编码:
我在向 Spring API 发送变音符号时遇到问题。我想 post 关注 JSON:
{
"username": "testümlaut",
"firstName": "test",
"lastName": "Test"
}
为此,我有以下方法开始:
@RequestMapping(value="/User", method=RequestMethod.POST,
produces={"application/json ; charset=utf-8"}
)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody User postUser(@RequestBody User user) {
User user = userDao.addUser(user);
return user;
}
如你所见,我有一行:
produces={"application/json ; charset=utf-8"}
但这并没有帮助。我总是遇到异常(0xfc 是 ü):
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Invalid UTF-8 start byte 0xfc
at [Source: java.io.PushbackInputStream@721e5ed1; line: 2, column: 19] (through reference chain: de.escosautomation.restserver.model.user.UserClone["username"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xfc
at [Source: java.io.PushbackInputStream@721e5ed1; line: 2, column: 19] (through reference chain: de.escosautomation.restserver.model.user.UserClone["username"])
我还可以添加什么来让它发挥作用?
谢谢。
您可以在此处检查几个选项:
在您的应用程序服务器上编码。例如,在 Tomcat 上,在所有连接器上确保将 URIEncoding 设置为 UTF-8。例如:
<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" URIEncoding="UTF-8"/>
- 当您发送 JSON 时,您需要 消耗和
产生设置为
MediaType.APPLICATION_JSON_UTF8_VALUE
(application/json;字符集=UTF-8)。例如:
@RequestMapping(value="/User", method=RequestMethod.POST,
consumes=MediaType.APPLICATION_JSON_UTF8_VALUE,
produces=MediaType.APPLICATION_JSON_UTF8_VALUE
)
在web.xml:
<filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
其实我接受尤里·尤尼科夫的回答。
但是如果有人在使用 SOAPui,请不要忘记在那里设置请求的编码: