多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
如果发现前后端传递的字符串中文乱码,可能是因为 SpringMVC 的消息转换器StringHttpMessageConverter固定的编码方式为ISO-8859-1。我们可以如下设置编码格式为`utf-8`防止中文乱码。 <br/> **方案1:设置属性`produces`** ```java @Controller public class TestController { @ResponseBody @GetMapping(value = "/json/user", produces = {"application/json;charset=UTF-8"}) public String addUser(@RequestParam("message") String message) { System.out.println(message); } } ``` <br/> **方案2:在`resources/springmvc-servlet.xml`配置消息转换器的编码** ```xml <mvc:annotation-driven> <!-- 配置消息转换器--> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value> application/json;charset=UTF-8 </value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> ```