4
回答
spring mvc @ResponseBody 乱码
注册华为云得mate10,2.9折抢先购!>>>   
package com.sinaapp.zhangziyou.action.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinaapp.zhangziyou.dao.TestDao;

/**
 * @author 作者 E-mail:zhangshanhai123@gmail.com
 * @version 创建时间:2013-9-5 下午1:55:13
 * 类说明
 */
@Controller
public class HelpController extends ApiController {
	
	
	@Autowired
	private TestDao testDao;
	@RequestMapping(value = "/qwerty/{id}", method = RequestMethod.GET)
	@ResponseBody
	
	
	// TODO 乱码问题没有解决掉。
	public ResponseEntity testResponse(@RequestBody String json) {
		System.out.println("--------checkUser--------");
		HttpHeaders headers = new HttpHeaders();
		headers.add("Location", "http://stackoverflow.com");
		ResponseEntity re = new ResponseEntity(json+"中文", headers, HttpStatus.OK);
		System.out.println( json + "——————————————————中文");
		
		return re;
	}
	
	@RequestMapping(value = "/help/test", method = RequestMethod.POST)
	@ResponseBody
	//  乱码问题没有解决掉。
	public ResponseEntity<String> test() {
		System.out.println("--------冒烟--------");
		HttpHeaders headers = new HttpHeaders();
	if(testDao.test()==1){
		ResponseEntity<String> re = new ResponseEntity<String>("{\"冒烟true\"}", headers, HttpStatus.OK);
		return re;
	}else{
		ResponseEntity<String> re = new ResponseEntity<String>("{\"冒烟false\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
		return re;
	}
		
	}
}
<?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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
	default-lazy-init="true">

	
	<!-- 拦截器 -->
	<mvc:interceptors>
		<bean class="com.sinaapp.zhangziyou.interceptors.WebDefaultInterceptor" />
		<mvc:interceptor>
			<mvc:mapping path="/u/**" />
			<bean class="com.sinaapp.zhangziyou.interceptors.UAuthorizeInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/" p:suffix=".jsp" />

	
	
	<!-- <bean class = "com.sinaapp.zhangziyou.common.EncodingPostProcessor" 
		/> -->
		<bean id="stringHttpMessageConverter"	class="org.springframework.http.converter.StringHttpMessageConverter">

			<property name="supportedMediaTypes">
				<list>
					<value>text/html;charset=UTF-8</value>
					<value>text/plain;charset=UTF-8</value>
					<value>application/json;charset=UTF-8</value> 
				</list>
			</property>
		</bean>
		
	
		
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="stringHttpMessageConverter" />
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>

	</bean>
<mvc:annotation-driven />
<tx:annotation-driven />

	<context:annotation-config />
	<aop:aspectj-autoproxy proxy-target-class="true" />
	

	<mvc:default-servlet-handler />

	<context:component-scan base-package="com.sinaapp.zhangziyou"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	
	<!-- <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/> -->
</beans>

别在意那个dao 注入到action里哈。那个是冒烟测试。

配置文件和action都在里面,为啥返回是乱码呢?

举报
顶部