日期:2014-05-16  浏览次数:20356 次

Spring MVC返回Json对象
pom.xml
    <dependency>  
        <groupId>org.codehaus.jackson</groupId>  
        <artifactId>jackson-core-asl</artifactId>  
        <version>1.9.4</version>  
    </dependency>  
    <dependency>  
        <groupId>org.codehaus.jackson</groupId>  
        <artifactId>jackson-mapper-asl</artifactId>  
        <version>1.9.4</version>  
    </dependency>  


applicationContext.xml
        <context:component-scan base-package="com.xx.wdss.web.controller" />

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


Controller:
	@RequestMapping(value="/test")  
    @ResponseBody  
    public Object test(HttpSession session){  
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("a", "a");
		map.put("b", 123);
		map.put("c", new Object[]{"cc",456});
        System.out.println("test....................");  
        return map;  
    }