Solve the problem that Spring MVC @ResponseBody returns garbled Chinese strings.
The reason is that the default processing string encoding used by spring mvc is ISO-8859-1. For details, refer to public static final Charset DEFAULT_CHARSET = in the org.springframework.http.converter.StringHttpMessageConverter class Charset.forName("ISO-8859-1");
Solution:
The first method:
Add annotations to methods that need to return strings, as follows:
@RequestMapping(value="/getUsers", produces = "application/json; charset=utf-8")
public String getAllUser() throws JsonGenerationException, JsonMappingException, IOException
{
List<User> users = userService.getAll();
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(users ));
DataGrid dg = new DataGrid();
dg.setData(users);
return om.writeValueAsString(dg);
}
This method only works for a single calling method.
The second method:
add
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name to the configuration file ="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Solve the problem that Spring MVC @ResponseBody returns garbled Chinese strings
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326450450&siteId=291194637
Ranking