@RequestMapping(value = "test", method = {
RequestMethod.GET, RequestMethod.POST
})
@ResponseBody
public String getMenuByParentId(Model model, String parentId) {
List<Menu> list = menuService.getByParentId(Long.valueOf(parentId));
StringBuffer sb = new StringBuffer();
sb.append("中文");
return sb.toString();
}
以上代码在请求时中文将会出现乱码,解决方法:
在Spring配置文件中加上以下代码。注意加在<mvc:annotation-driven />之前
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- Json返回 乱码处理 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
</list>
</property>
</bean>