码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC学习(11)-json数据交互和RESTful支持

时间:2017-01-21 21:09:32      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:ems   java   传输   技术   ret   row   调用   方便   com   

一、json数据交互:

json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。

比如:webservice接口,传输json数据.

springMVC进行json交互

技术分享

1)环境准备:

加载json转换的jar包:

springmvc中使用jackson的包进行json转换(@requestBody和@responseBody使用下边的包进行json转)

jackson-core-asl-1.9.11.jar

jackson-mapper-asl-1.9.11.jar

2)配置json转换器;

如果是配置单个的注解适配器,要加入下面配置:

技术分享
1 <!--注解适配器 -->
2     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
3         <property name="messageConverters">
4         <list>
5         <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
6         </list>
7         </property>
8     </bean>
View Code

如果使用<mvc:annotation-driven /> 则不用定义上边的内容。

3)代码实现:测试:

 1 package com.cy.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestBody;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 
 9 import com.cy.po.ItemsCustom;
10 import com.cy.service.ItemsService;
11 
12 /**
13  * json交互测试
14  *
15  */
16 @Controller
17 public class JsonTest {
18     
19     @Autowired
20     private ItemsService itemsService;
21     
22     //请求json串(商品信息),输出json(商品信息)
23     //@RequestBody将请求的商品信息的json串转成itemsCustom对象
24     @RequestMapping("/requestJson")
25     @ResponseBody
26     public  ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom) throws Exception{
27         int id = itemsCustom.getId();
28         ItemsCustom itemsCustom2 = itemsService.findItemsById(id);
29         return itemsCustom2;
30     }
31     
32     //请求key/value,输出json
33     //@ResponseBody将itemsCustom转成json输出
34     @RequestMapping("/responseJson")
35     @ResponseBody
36     public ItemsCustom responseJson(ItemsCustom itemsCustom) throws Exception{
37         int id = itemsCustom.getId();
38         ItemsCustom itemsCustom2 = itemsService.findItemsById(id);
39         return itemsCustom2;
40     }
41 }

jsp页面:

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>json交互测试</title>
 8 <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
 9 <script type="text/javascript">
10 //请求json,输出是json
11 function requestJson(){
12     $.ajax({
13         type:‘post‘,
14         url:‘${pageContext.request.contextPath }/requestJson.action‘,
15         contentType:‘application/json;charset=utf-8‘,
16         //数据格式是json串,商品信息
17         //这边很严格的做了测试,必须是这种格式;
18         //key加引号;整个{}加引号;整个就是一个json串;有点像JOSN.stringify(JOSNObject)这样子
19         data:‘{"id":1,"name":"手机","price":999}‘,
20         success:function(data){
21             console.log(data);
22         }
23         
24     });
25 }
26 //请求key/value,输出是json
27 function responseJson(){
28     $.ajax({
29         type:‘post‘,
30         url:‘${pageContext.request.contextPath }/responseJson.action‘,
31         data:‘id=4&name=手机&price=999‘,
32         success:function(data){//返回json结果
33             console.log(data);
34         }
35     });
36 }
37 </script>
38 </head>
39 <body>
40 <input type="button" onclick="requestJson()" value="请求json,输出是json"/>
41 <input type="button" onclick="responseJson()" value="请求key/value,输出是json"/>
42 </body>
43 </html>
View Code

测试结果:

数据库内容:

技术分享

requestJson:

技术分享

responseJson:

技术分享

 

 

二、RESTful支持:

 

springMVC学习(11)-json数据交互和RESTful支持

标签:ems   java   传输   技术   ret   row   调用   方便   com   

原文地址:http://www.cnblogs.com/tenWood/p/6337633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!