springmvc 输入输出json类型数据
2014-03-02 22:34
399 查看
Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改pom.xml增加jackson包的引用 <!-- json -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
先修改之前的helloworld.jsp,增加客户端json格式的数据输入。
根据前面的分析,在spring mvc中解析输入为json格式的数据有两种方式 1:使用@RequestBody来设置输入
2:使用HttpEntity来实现输入绑定
Json格式的输出也对应有两种方式 1:使用@responseBody来设置输出内容为context body 2:返回值设置为ResponseEntity<?>类型,以返回context body 另外,第三种方式是使用ContentNegotiatingViewResolver来设置输出为json格式,需要修改servlet context配置文件如下
但这种格式的输出会返回{model类名:{内容}} 的json格式, 例如,以下代码
期望的返回是 {success:true,message:”return ok”}; 但实际返回的却是 {"jsonResult":{"success":true,"msg":"return ok"}} 原因是MappingJacksonJsonView中对返回值的处理未考虑modelMap中只有一个值的情况,直接是按照mapName:{mapResult}的格式来返回数据的。
修改方法,重载MappingJacksonJsonView类并重写filterModel方法如下
对应的ContentNegotiatingViewResolver修改如下
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.8.1</version>
</dependency>
先修改之前的helloworld.jsp,增加客户端json格式的数据输入。
var cfg = { type: 'POST', data: JSON.stringify({userName:'winzip',password:'password',mobileNO:'13818881888'}), dataType: 'json', contentType:'application/json;charset=UTF-8', success: function(result) { alert(result.success); } }; function doTestJson(actionName){ cfg.url = actionName; $.ajax(cfg); }
根据前面的分析,在spring mvc中解析输入为json格式的数据有两种方式 1:使用@RequestBody来设置输入
@RequestMapping("/json1") @ResponseBody public JsonResult testJson1(@RequestBody User u){ log.info("get json input from request body annotation"); log.info(u.getUserName()); return new JsonResult(true,"return ok"); }
2:使用HttpEntity来实现输入绑定
@RequestMapping("/json2") public ResponseEntity<JsonResult> testJson2(HttpEntity<User> u){ log.info("get json input from HttpEntity annotation"); log.info(u.getBody().getUserName()); ResponseEntity<JsonResult> responseResult = new ResponseEntity<JsonResult>( new JsonResult(true,"return ok"),HttpStatus.OK); return responseResult; }
Json格式的输出也对应有两种方式 1:使用@responseBody来设置输出内容为context body 2:返回值设置为ResponseEntity<?>类型,以返回context body 另外,第三种方式是使用ContentNegotiatingViewResolver来设置输出为json格式,需要修改servlet context配置文件如下
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> </map> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </list> </property> <property name="ignoreAcceptHeader" value="true" /> </bean>
但这种格式的输出会返回{model类名:{内容}} 的json格式, 例如,以下代码
@RequestMapping("/json3.json") public JsonResult testJson3(@RequestBody User u){ log.info("handle json output from ContentNegotiatingViewResolver"); return new JsonResult(true,"return ok"); }
期望的返回是 {success:true,message:”return ok”}; 但实际返回的却是 {"jsonResult":{"success":true,"msg":"return ok"}} 原因是MappingJacksonJsonView中对返回值的处理未考虑modelMap中只有一个值的情况,直接是按照mapName:{mapResult}的格式来返回数据的。
修改方法,重载MappingJacksonJsonView类并重写filterModel方法如下
protected Object filterModel(Map<String, Object> model) { Map<?, ?> result = (Map<?, ?>) super.filterModel(model); if (result.size() == 1) { return result.values().iterator().next(); } else { return result; } }
对应的ContentNegotiatingViewResolver修改如下
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> </map> </property> <property name="defaultViews"> <list> <bean class="net.zhepu.json.MappingJacksonJsonView" /> </list> </property> <property name="ignoreAcceptHeader" value="true" /> </bean>
相关文章推荐
- Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器
- 我的Python学习之路 Python的输入输出与基本数据类型
- springmvc接收复杂类型的json数据需要注意的地方
- [TwistedFate]C语言学习:C数据类型,常量变量,运算符,表达式,输入输出
- C语言基础:进制转换,变量,常量,表达式,基本数据类型,输出函数,输入函数,运算符.
- 实用C#中任意类型数据转成JSON格式数据输出
- SpringMVC返回JSON数据中日期类型数据的格式化
- Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器
- C/C++之类型输出,乘法表,古典问题,数据输入输出
- springmvc支持json和jsonp类型数据
- C语言基础学习基本数据类型-字符专属的输入输出函数
- SpringMVC中在使用@ResponseBody时如何将Date类型的日期返回指定格式的json数据
- 了解Hadoop数据类型,输入输出格式及用户如何自定义。
- 基本数据类型 运算符 输入输出语句
- python 输出JSON类型数据时遇到的编码问题(utf8,unicode)
- 数据类型uint16_t,uint32_t,uint64_t输入输出使用方法
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- C#中任意类型数据转成JSON格式数据输出
- Android JNI编程(二)——C语言的基本数据类型,输出函数,输入函数
- 2.C#的输入、输出与运算符、数据类型