Spring MVC response encoding issue
Solution 1
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.
Solution 2
Instead @ResponseBody use ResponseEntity.
@RequestMapping(value="test")
public ResponseEntity<String> test(){
String test = "čćžđš";
System.out.println(test);
logger.info(test);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "application/json; charset=UTF-8");
return ResponseEntity<String>(test,responseHeaders, HttpStatus.OK);
}
Solution 3
My simple solution:
@RequestMapping(value="test")
public ModelAndView test(){
String test = "čćžđš";
...
ModelAndView mav = new ModelAndView("html_utf8");
mav.addObject("responseBody", test);
}
and the view html_utf8.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>${responseBody}
No additional classes and configuration.
And You can also create another view (for example json_utf8) for other content type.
Comments
-
ant over 1 year
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
istext/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.
-
ant almost 13 yearsthanks, your answer also got me thinking and eventually to solution.
-
ant almost 13 yearsthansk but I'm not using this service to produce html at the end. I consume it from android phone, sending it json string as plain text