发表于: 2018-03-28 23:37:28
1 544
今天完成的事情:
1. 什么是restful?rest的请求方法有哪些,有什么区别?
答:表征性状态传输(英文:Representational State Transfer,简称REST)是一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件,如果一个架构符合REST原则,我们就称它为RESTful架构。
RESTful架构:(1)每一个URI代表一种资源;(2)客户端和服务器之间,传递这种资源的某种表现层;(3)客户端通过HTTP四个动词,对服务器资源进行操作,实现“表现层状态转化“。
请求方法有:GET,POST,DELETE,PUT。GET用来获取资源,POST用来新建资源也可以更新资源,PUT用来更新资源,DELETE用来删除资源。
2. 为什么要用Rest风格,如果不用Rest的话,接口应该怎么定义,在使用Rest风格之前,大家都是用什么方式写接口的?
答:网络应用程序,分为前端和后端两个部分,通过互联网通信,具有高延时(high Iatency)、高并发等特点。当前的发展趋势,前端设备层出不穷(手机、平板、电脑、其他专用设备等)。因此需要有一种统一的机制,方便不同的前端设备与后端进行通信,这就导致API架构的流行。
REST是省略了主语resource的,resource就是网络上的一个实体或者是网络上的一个具体信息,比如是文本、图片、歌曲、服务等。在网络上可以用URI(统一资源定位符)指向它,每个资源对应一个特定的URI,要获取这个资源只需要访问它的URI,此时URI就成了每一个资源的地址或独一无二的识别符。
Representation就是资源resource的表现形式,称为“表现层”。
而资源的表现形式就是它的各种状态的体现,不同表现形式的转换就是状态转化(state transfer)。状态转化发生在客户端和服务器交互的过程中。而HTTP协议是无状态协议,所以所有状态都保存在服务器端,因此,客户端想要操作服务器,必须通过某种手段,让服务器端发生state transfer,而这种转化是建立在表现层之上的,所以就是“Representation State Transfer”。
客户端用到的手段,只能是HTTP协议中表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET用来获取资源,POST用来新建资源也可以更新资源,PUT用来更新资源,DELETE用来删除资源。
(http://www.ruanyifeng.com/blog/2011/09/restful.html)
3.之前的url不符合RESTful改下
@Controller
@RequestMapping("/test")
public class StudentController {
@Autowired
private StudentService studentService;
//查询
@RequestMapping(value = "/student/{id}",method = RequestMethod.GET)
public String getStudentById(@PathVariable("id") long id, Model model) throws Exception{
Student student =studentService.getStudentById(id);
model.addAttribute("student" ,student);
return "studentEdit";
}
//分页显示
@RequestMapping(value = "/students",method = RequestMethod.GET)
public String getStudentByPage(@RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage,Model model) throws Exception{
System.out.println(currentPage);
model.addAttribute("pagemsg",studentService.getStudentsByPage(currentPage));
return "studentList";
}
//新增
@RequestMapping(value = "/newOne",method = RequestMethod.GET)
public String saveStudent() throws Exception{
return "studentInput";
}
@RequestMapping(value = "/newOne",method = RequestMethod.POST)
public String saveStudent(Student student) throws Exception{
studentService.saveStudent(student);
return "redirect:/test/students";
}
//更新
// @RequestMapping(value = "/update/{id}",method = RequestMethod.GET)
// public String updateStudent(@PathVariable("id") long id,Student student) throws Exception{
// studentService.getStudentById(id);
// return "student";
// }
@RequestMapping(value = "/student/{id}",method = RequestMethod.PUT)
public String updateStudnet(Student student) throws Exception{
studentService.updateStudent(student);
return "redirect:/test/students";
}
//删除
@RequestMapping(value = "/student/{id}",method = RequestMethod.DELETE)
public String deleteStudent(@PathVariable("id") long id) throws Exception{
studentService.deleteStudentById(id);
return "redirect:/test/students";
}
//通过姓名查询
@RequestMapping(value = "/student",method = RequestMethod.GET)
public String getStudentsByName(@RequestParam String name,Model model) throws Exception{
List<Student> list=studentService.getStudentsListByName(name);
model.addAttribute("list",list);
return "studentSearch";
}
}
明天的计划:
深度思考
遇到的问题:
新增的方法还没有改好,明天改下。
收获:
学习REST和RESTful.
评论