Spring MVC uses @ResponseBody annotation to return Chinese string garbled problem

In the project, I have encountered the problem that the string returned by Spring MVC using the @ResponseBody annotation contains garbled Chinese strings.
Mainly, the matching string is found in the string returned by Spring mvc parsing. The StringHttpMessageConverter class parses the string, which is used by default.
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

Solution:
1. Add produces = "application/json; charset=utf-8" to the method that needs to return a string. For a single method
such as :
@RequestMapping(value = "/getInfos", produces="application/json;charset=UTF-8")
 @ResponseBody
 public String getInfos(@RequestBody String request) {
      return "Chinese";
    }

2. Configure the interceptor in the spring mvc configuration file
<mvc:annotation-driven>  
    <mvc:message-converters register-defaults="true">  
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
            <property name="supportedMediaTypes" value = "text/html;charset=UTF-8" />  
        </bean>  
    </mvc:message-converters>  
</mvc:annotation-driven>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326836432&siteId=291194637