I'm having in my Annotation driven Spring MVC Java web application runned on jetty web server (currently in maven jetty plugin).
I'm trying to do some AJAX support with one controller method returning just String help text. Resources are in UTF-8 encoding and so is the string, but my response from server comes with
content-encoding: text/plain;charset=ISO-8859-1
even when my browser sends
Accept-Charset windows-1250,utf-8;q=0.7,*;q=0.7
I'm using somehow default configuration of spring
I have found a hint to add this bean to the configuration, but I think it's just not used, because it says it does not support the encoding and a default one is used instead.
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
My controller code is (note that this change of response type is not working for me):
@RequestMapping(value = "ajax/gethelp")
public @ResponseBody String handleGetHelp(Locale loc, String code, HttpServletResponse response) {
log.debug("Getting help for code: " + code);
response.setContentType("text/plain;charset=UTF-8");
String help = messageSource.getMessage(code, null, loc);
log.debug("Help is: " + help);
return help;
}
The simple way to solve this problem in Spring 3.1.1 is that: add following configuration codes in
servlet-context.xml
Don't need to override or implement anything.
I was fighting this issue recently and found a much better answer available in Spring 3.1:
So, as easy as JAX-RS just like all the comments indicated it could/should be.
I set the content-type in the MarshallingView in the ContentNegotiatingViewResolver bean. It works easily, clean and smoothly:
According to the link " If a character encoding is not specified, the Servlet specification requires that an encoding of ISO-8859-1 is used ".If you are using spring 3.1 or later use the fallowing configuration to set charset=UTF-8 to response body
@RequestMapping(value = "your mapping url", produces = "text/plain;charset=UTF-8")