您的位置:首页 > 其它

Rest ful API的一些基本概念

2016-03-28 10:26 316 查看

0,应用背景

      Rest ful api是  Fielding(楼下那叔叔) 在他的论文《Architectural Styles and the Design of Network-based Software Architectures》中提出的,论文地址:http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm。(

。。。)

  


       因为最近在做hybird App,需要调用使用Sping mvc发布的接口,就想尝试一下新的接口形式,希望能规范下接口开发,方便调用,具体都有什么好效果,再议。

二,REST ful 基本概念

                      REST:Representational State Transfer,即表现层状态转换。在网络上,我们通过URI定位资源,之后将资源呈现出来的具体形式叫做它的“表现层”。

                     理解状态转换:
                                  

 

   三,URI的潜规则

                 在REST ful API设计中,客户端通过四个HTTP动词,对服务器端资源进行操作,实现"表现层状态转化",所以,可以看出我们对每一个URL中代表的资源操作都放在了http请求中,所以,在URL中,不应该再包含有动词。

            代表资源的URI应该只包含名词。

       

  四,常用HTTP动词

                  

五,关于前端和后台的交互实现

          前端对于API数据的操作放在了我们AngularJs的service模块中,通过
$resource
来访问我们的API接口,实现对数据的CRUD操作,其实感觉跟我们JQ中的AJAX是差不多的,但是实现起来当然还有一些小细节要注意。

 
       
    在后台,主要是在Spring Mvc的Controller中,对于方法,我们建议返回JSON数据,通过@ResponseBody注解标注方法,并引入JackJson的Jar包,在spring mvc的配置文件中加入可返回JSON的配置。(根据Spring MVC  的版本而定,如下,为了分离前后台开发,接口对接完成之后,又后台先快速开发一套假数据的项目,快速部署,方便前端调用)。
    @Controller
@RequestMapping("/curriculumschedule")
public class GetDataController {

/*测试发布*/
@RequestMapping("/mvc")
public String helloMvc() {
return "home";
}

/*登陆接口*/
@ResponseBody
@RequestMapping(value="/login/userCode/{userCode}/password/{password}",method=RequestMethod.GET)
public String login(@PathVariable("userCode") String userCode,@PathVariable("password") String password,HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin", "*"); //允许哪些url可以跨域请求到本域
System.out.println("用户名为:"+userCode);
System.out.println("密码为:"+password);
return "{\"result\":true,\"allUsers\":{\"id\":\"12EBA23Qsew345\",\"userName\":\"二牛\",\"userCode\":\"1016685862\",\"password\":\"1\",\"headImage\":\"img/01.jpg\",\"roleType\":0}}";
}

/*查询今日课表*/
@ResponseBody
@RequestMapping(value="/todayLession/studentCode/{studentCode}",method=RequestMethod.GET)
public String todayLession(@PathVariable("studentCode") String studentCode,HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin", "*"); //允许哪些url可以跨域请求到本域
System.out.println("学生code为:"+studentCode);
return "{\"result\":true,\"DayCourse\":[{\"startTime\":\"08:00\",\"endTime\":\"10:00\",\"courseName\":\"高等数学\",\"teacherName\":\"王玉兰\",\"roomName\":\"11教501\"},{\"startTime\":\"10:00\",\"endTime\":\"12:00\",\"courseName\":\"英语\",\"teacherName\":\"杨兰\",\"roomName\":\"12教224\"},{\"startTime\":\"14:00\",\"endTime\":\"16:00\",\"courseName\":\"人体解剖学\",\"teacherName\":\"高峰\",\"roomName\":\"10教104\"},{\"startTime\":\"16:00\",\"endTime\":\"18:00\",\"courseName\":\"生物科学\",\"teacherName\":\"刘二\",\"roomName\":\"6教302\"}]}";
}

/*查询本周课表*/
@ResponseBody
@RequestMapping(value="/weekLession/studentCode/{studentCode}",method=RequestMethod.GET)
public String weekLession(@PathVariable("studentCode") String studentCode,HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin", "*"); //允许哪些url可以跨域请求到本域
System.out.println("学生code为:"+studentCode);
return "{\"result\":true,\"ItemWeekIndex\":\"第二周\",\"Content\":[{\"weekIndex\":1,\"courseInfo\":\"11教501高等数学\",\"periodTimesIndex\":1},{\"weekIndex\":6,\"courseInfo\":\"11教232大学英语(一)\",\"periodTimesIndex\":2},{\"weekIndex\":7,\"courseInfo\":\"11教232食品安全学\",\"periodTimesIndex\":4},{\"weekIndex\":2,\"courseInfo\":\"11教232乳制品工艺学\",\"periodTimesIndex\":1},{\"weekIndex\":2,\"courseInfo\":\"11教232大学英语(二)\",\"periodTimesIndex\":2},{\"weekIndex\":2,\"courseInfo\":\"11教232计算机基础(一)\",\"periodTimesIndex\":4},{\"weekIndex\":3,\"courseInfo\":\"11教234人体解剖学\",\"periodTimesIndex\":2},{\"weekIndex\":3,\"courseInfo\":\"11教234植物资源学\",\"periodTimesIndex\":3},{\"weekIndex\":3,\"courseInfo\":\"11教234花卉栽培技术\",\"periodTimesIndex\":4},{\"weekIndex\":4,\"courseInfo\":\"11教234植物衰老生物学\",\"periodTimesIndex\":2},{\"weekIndex\":5,\"courseInfo\":\"11教234文献检索\",\"periodTimesIndex\":3}]}";
}

         关于移动端和后台开发的分离,也可以使用野狗模拟的假数据,替代了使用本地JSON的形式。前段时间看NodeJs的时候,发现NodeJS里面有个模块faker,也有这方面的效果,但是还木有具体尝试,但是使用NodeJS进行开发也能做到两端分离开发。等有时间了再研究下,有用过的大神们可以留个言。

             

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: