[英]Who sets request content-type in Spring MVC
I have read through the post " Who sets response content-type in Spring MVC (@ResponseBody) ", it helped me to resolve my problem of displaying UTF-8 (CJK characters) data at client side using JSON method. 我已经阅读了“ 谁在Spring MVC中设置响应内容类型(@ResponseBody) ”一文,它帮助我解决了使用JSON方法在客户端显示UTF-8(CJK字符)数据的问题。
Now find I have problem to post UTF-8 data to server side using JSON. 现在发现我有使用JSON将UTF-8数据发布到服务器端的问题。 The javascript method I am using: 我正在使用的javascript方法:
function startSomething() {
console.log("startSomething()");
console.log(" getOriginName() = " + getOriginName());
console.log(" getDestinationName() = " + getDestinationName());
$.ajaxSetup({ scriptCharset: "utf-8" ,
contentType: "application/json; charset=utf-8"});
// send the data to server side
$.ajax({
url: "/mywebapp/something/start",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {
originName : getOriginName(),
destinationName : getDestinationName()
},
success: function(response) {
// do something
}
});
}
After triggering the javascript method, I can see the value printed out in browser console correctly, something as: 触发javascript方法后,我可以在浏览器控制台中正确看到打印出的值,如下所示:
getOriginName() = N Bridge Rd
getDestinationName() = 夢幻之城@ Boat Quay
My server side code: 我的服务器端代码:
@RequestMapping("/something")
@Controller
public class TestController {
// the logger
private Logger logger = Logger.getLogger(TestController.class);
@RequestMapping(value = "/start", method = RequestMethod.GET)
public ResponseEntity<String> start(@RequestParam String originName,
@RequestParam String destinationName,
HttpServletRequest request,
HttpServletResponse response) {
String characterEncoding = request.getCharacterEncoding();
String contentType = request.getContentType();
logger.debug(" characterEncoding = " + characterEncoding);
logger.debug(" contentType = " + contentType);
if (logger.isDebugEnabled()) {
String logMessage = StringUtils.join(
new Object[]{
" originName = ", originName,
" destinationName = ", destinationName
}
);
logger.debug(logMessage);
}
...
}
}
The output from my server side code: 我的服务器端代码的输出:
TestController - characterEncoding = UTF-8
TestController - contentType = application/json; charset=utf-8
TestController - originName = N Bridge Rd destinationName = 夢幻ä¹å@ Boat Quay
You can see that the request encoding is UTF-8, but the value from client side is wrongly encoded CJK character. 您可以看到请求编码为UTF-8,但是来自客户端的值是错误编码的CJK字符。
What can go wrong here? 这里可能出什么问题? Please give me some hints, thank you. 请给我一些提示,谢谢。 George 乔治
I had a similar problem with JSON & Spring and solved the issue by specifying URIEncoding="UTF-8"
on <Connector>
in my Tomcat server.xml config, as described here: 我在JSON和Spring中也遇到了类似的问题,并通过在Tomcat server.xml配置中的<Connector>
上指定URIEncoding="UTF-8"
解决了该问题,如下所述:
http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8 http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.