spring4.x mvc @ResponseBody 标签乱码问题解决方法

step1  . 在spring 配置文件中加入

	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">

			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg value="UTF-8" />
			</bean>
			<bean
				class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<property name="features">
					<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
						<value>DisableCircularReferenceDetect</value>
					</array>
				</property>
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
						<value>text/html;charset=UTF-8</value>
						<value>text/plain;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

  step 2.web.xml中添加

	<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

  上述都无效可以试试在注解中加入

	@ResponseBody
	@RequestMapping(value = "/showContentById" ,produces = {"application/json;charset=UTF-8"})

  

 

posted @ 2017-03-21 15:31  一明哥  阅读(314)  评论(0)    收藏  举报