In Spring MVC, the @ResponseBody annotation plays a crucial role in determining the content type of the response sent back to the client. This annotation is useful when returning custom responses, such as JSON or plain text, in a flexible manner.
In your case, you're encountering an issue where the response from your controller method is being set with an incorrect content-encoding. This is due to the default behavior of Spring MVC, which falls back to the ISO-8859-1 encoding if it doesn't find a suitable converter for the returned value.
To resolve this problem and set the correct content type, you can utilize the produces attribute of the @ResponseBody annotation. This attribute allows you to specify the media types (i.e., content types) that your controller method can produce.
For your specific case, you want your controller method to produce plain text in UTF-8 encoding. Here's how you can achieve this:
@RequestMapping(value = "ajax/gethelp")
@ResponseBody
public String handleGetHelp(Locale loc, String code) {
log.debug("Getting help for code: " code);
String help = messageSource.getMessage(code, null, loc);
log.debug("Help is: " help);
return help;
}
By adding produces = "text/plain; charset=utf-8" to the @ResponseBody annotation, you're instructing Spring MVC to explicitly set the content type of the response to text/plain with a character set of utf-8. This should ensure that the response is encoded correctly and displayed properly on the client side.
Note that the @RequestMapping annotation also supports the produces attribute, which can be used to control the media types that the corresponding request mapping can handle.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3