跪求解决springmvc responsebody返回中文乱码

RHuniSoft 2018-08-11 04:10:51
springmvc responsebody返回中文乱码,注解里添加了produces="application/json;charset=utf-8"也不起作用
	@RequestMapping(value="/de", method=RequestMethod.GET, produces="application/json;charset=utf-8")
@ResponseBody
public String select(){
return "你好";
}

web.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>网关</display-name>
<welcome-file-list>
<welcome-file>login.do</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>


springmvc的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
...全文
431 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
青灯古酒ி 2018-08-14
  • 打赏
  • 举报
回复
用eclipse内置浏览器不乱码是因为eclipse的编码方式和你的代码编码方式是一致的。
解决方法有两个:第一是设置contentType
第二是修改tomcat配置文件,具体修改方式自行百度
tandy 2018-08-14
  • 打赏
  • 举报
回复
在 Eclipse Package Explorer 中 找到 Servers
点击你的 tomcat
找到 server.xml
找到 <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
如上 加上 URIEncoding="UTF-8"

这样你在发送 get 请求返回的中文字符就不是乱码了
天命ming 2018-08-12
  • 打赏
  • 举报
回复
你可以进入调试界面看看,到底返回的json数据是不是乱码
hi-face 2018-08-12
  • 打赏
  • 举报
回复
mvc_context.xml 添加

<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>


史上最简单的spring+springMVC+mybaits整合SSM框架~Simple-SSM

zzxiaoma 2018-08-11
  • 打赏
  • 举报
回复
你试试在乱码的地方点右键选择utf-8
RHuniSoft 2018-08-11
  • 打赏
  • 举报
回复
引用 1 楼 zzxiaoma 的回复:
客户端的问题吧,有时候用浏览器直接访问就是乱码,用程序就没问题


我是在浏览器直接输url 返回你好 在eclipse自带的浏览器不乱码
但是外边所有的浏览器都乱码 这是怎么回事 j2ee不熟
zzxiaoma 2018-08-11
  • 打赏
  • 举报
回复
客户端的问题吧,有时候用浏览器直接访问就是乱码,用程序就没问题

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧