第一步:在需要访问的controller层的接口上添加@ResponseBody注释 如下所示
加上以上配置之后重启服务 中文正常显示
@Controller
@RequestMapping("myportalAndParty")
public class MyportalAndParty {
@RequestMapping(value = "QueryPartyByMyportal", method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public String QueryPartyByMyportal(HttpServletRequest request, HttpServletResponse response){
return null;
}
}
第二部:在spring-servlet.xml中添加如下配置
<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>
</list>
</property>
</bean>第三步:在第二部的后面加上下面的一行配置 必须加上 不然以上的配置不会生效
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />加上以上配置之后重启服务 中文正常显示
本文介绍如何在Spring MVC中配置正确的消息转换器以支持中文显示,并确保RESTful API能正确响应中文内容。通过具体步骤说明如何设置@Controller和@ResponseBody注解,并在spring-servlet.xml文件中添加必要的@Bean配置。
467

被折叠的 条评论
为什么被折叠?



