问题描述
在过去的几个小时里,我阅读了很多关于这个主题的文章,但到目前为止没有任何效果.我正在尝试返回包含奇数"某些字符的响应.这是一个例子,很简单:
@ResponseBody@RequestMapping(值=测试")公共字符串测试(){字符串测试 = "čćžđš";System.out.println(测试);记录器信息(测试);返回测试;}
这是我的
我认为正确的方法是返回 UTF-8,也许我错了.
在这几天之后,我有了谁是你爸爸的时刻".它来自阅读 spring 3.0 参考,我没有其他可尝试的,所以为什么不仔细阅读整个文档......以及@axtavt 答案的组合:
谁设置了响应内容类型Spring MVC (@ResponseBody)
更改原始解决方案:
公共类 EncodingPostProcessor 实现 BeanPostProcessor {公共对象 postProcessBeforeInitialization(对象 bean,字符串名称)抛出 BeansException {if (bean instanceof AnnotationMethodHandlerAdapter) {HttpMessageConverter>[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();for (HttpMessageConverter> conv: convs) {if (conv instanceof StringHttpMessageConverter) {((StringHttpMessageConverter) conv).setSupportedMediaTypes(Arrays.asList(new MediaType("text", "html",Charset.forName("UTF-8"))));}}}还豆;}
致:
公共类 EncodingPostProcessor 实现 BeanPostProcessor {公共对象 postProcessBeforeInitialization(对象 bean,字符串名称)抛出 BeansException {if (bean instanceof AnnotationMethodHandlerAdapter) {HttpMessageConverter>[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();for (HttpMessageConverter> conv: convs) {if (conv instanceof StringHttpMessageConverter) {((StringHttpMessageConverter) conv).setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain",Charset.forName("UTF-8"))));}}}还豆;}
该死的春天!!!但我还是会继续使用它.
In last few hours I've read a lot concerning this topic, and so far nothing has worked. I'm trying to return response containing "odd" some characters. Here is example of that, quite simple :
@ResponseBody
@RequestMapping(value="test")
public String test(){
String test = "čćžđš";
System.out.println(test);
logger.info(test);
return test;
}
This is my web.xml, because I found some answers where CharacterEncodingFilter helped(not in my case though). I used POST method because I read this applies to POST.
Also found this answer(related). Didn't help as well.
When I debug it the correct value appears, but when I print it doesn't as it can be seen below:
When I test it from jmeter, the response seems to be OK, Content-Type
is text/html;charset=UTF-8
Here is a screenshot of that as well. http://i56.tinypic.com/14lt653.jpg
I think the right way is to return UTF-8, maybe I'm wrong.
After few days of this I just had "who's your daddy moment". It came from reading spring 3.0 reference, I had nothing else to try so why not go trough entire documentation.. and combination of @axtavt answer :
Who sets response content-type in Spring MVC (@ResponseBody)
Changed original solution :
public class EncodingPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String name)
throws BeansException {
if (bean instanceof AnnotationMethodHandlerAdapter) {
HttpMessageConverter<?>[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();
for (HttpMessageConverter<?> conv: convs) {
if (conv instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) conv).setSupportedMediaTypes(
Arrays.asList(new MediaType("text", "html",
Charset.forName("UTF-8"))));
}
}
}
return bean;
}
To :
public class EncodingPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String name)
throws BeansException {
if (bean instanceof AnnotationMethodHandlerAdapter) {
HttpMessageConverter<?>[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();
for (HttpMessageConverter<?> conv: convs) {
if (conv instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) conv).setSupportedMediaTypes(
Arrays.asList(new MediaType("text", "plain",
Charset.forName("UTF-8"))));
}
}
}
return bean;
}
Darn spring!!! but still I'll continue to use it.
这篇关于Spring MVC 响应编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!