spring mvc 返回json的配置
转载自:http://my.oschina.net/haopeng/blog/324934
springMVC-servlet.xml 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
<!-- 启动Springmvc注解驱动 --> <mvc:annotation-driven/> <!-- 返回json 方法一 需要导入 fastjson.jar包 --> <mvc:annotation-driven> <mvc:message-converters register-defaults= "false" > <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id= "fastJsonHttpMessageConverter" class = "com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter" > <property name= "supportedMediaTypes" > <list> <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 --> <value>text/html;charset=UTF- 8 </value> <value>application/json;charset=UTF- 8 </value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 返回json 方法二 需要导入 jackson-annotations.jar,jackson-core.jar,jackson-databind.jar--> <!-- <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > <property name= "messageConverters" > <list> <bean class = "org.springframework.http.converter.StringHttpMessageConverter" > <property name= "supportedMediaTypes" > <list> <value>text/html; charset=UTF- 8 </value> <value>application/json;charset=UTF- 8 </value> </list> </property> </bean> <bean class = "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > <property name= "supportedMediaTypes" > <list> <value>text/html; charset=UTF- 8 </value> <value>application/json;charset=UTF- 8 </value> </list> </property> </bean> </list> </property> </bean> --> <!-- 其下与返回json 无关 --> <!-- 自动将控制器加载到bean --> <context:component-scan base- package = "org.controller" ></context:component-scan> <!-- 配置处理静态资源的请求 --> <mvc:resources location= "/resources/" mapping= "*/res/**" /> <!-- 拦截器配置 --> <mvc:interceptors> <bean class = "org.aop.LoginInteceptor" /> </mvc:interceptors> <!-- 配置视图解析器 --> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp/" /> <property name= "suffix" value= ".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name= "viewClass" value= "org.springframework.web.servlet.view.JstlView" /> </bean> <!-- 上传 需要使用到的配置--> <bean id= "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name= "maxUploadSize" value= "1046666000" /> </bean> <!--配置全局的异常--> <!-- <bean id= "exceptionResolver" class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > <property name= "exceptionMappings" > <props> <prop key= "com.guanlan.util.SportException" >error</prop> </props> </property> </bean> --> |
Controller.java 代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/** * 首页测试控制器 * @ClassName: HomeController * @Description: TODO(这里用一句话描述这个类的作用) * @author 郝鹏 * @date 2014-9-29 下午2:22:35 * */ @Controller public class HomeController { @RequestMapping ({ "/" }) public String index(Map<String, Object> map){ map.put( "name" , "郝鹏" ); return "index" ; } /** * 返回json * @Title: getJson * @Description: TODO(这里用一句话描述这个方法的作用) * @return * @return Object 返回类型 * @author 郝鹏 * @date 2014-10-8 下午3:50:24 */ @RequestMapping (value= "/json" ) @ResponseBody public Object getJson(){ Map<String, Object> map= new HashMap<String, Object>(); map.put( "fd" , "郝鹏" ); return map; } } |