Who sets response content-type in Spring MVC (@ResponseBody)

Hurda picture Hurda · Sep 1, 2010 · Viewed 260.3k times · Source

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;
}

Answer

Warrior picture Warrior · Aug 8, 2012

I found solution for Spring 3.1. with using @ResponseBody annotation. Here is example of controller using Json output:

@RequestMapping(value = "/getDealers", method = RequestMethod.GET, 
produces = "application/json; charset=utf-8")
@ResponseBody
public String sendMobileData() {

}