企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
REST和RESTFUL REST是一种架构风格,REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是 RESTful。其核心是面向资源。 ![](https://box.kancloud.cn/aedb19aa8eeed361a565fb612d79534a_600x388.png) 面向操作的URL /getAllUsers /getUser?userid=1 /getUser?userid=2 面向资源的URL: 1. URL中不体现操作,即没有动词。 2. 每个资源对应不同的URL, /user/1, /user/2 GET /users:列出所有用户 POST /users:新建一个用户 GET /user/ID:获取某个指定用户的信息 PUT /user/ID:更新某个指定用户的信息 DELETE /user/ID:删除某个用户 复杂一些的URL(注意,URL中的中文涉及URL编码的问题): Get /users/{username}/name Get /users/{username}/{password}/validate Get /users/2012/10 Rest常用的5个verb - GET(SELECT):从服务器取出资源(一项或多项)。 - POST(CREATE):在服务器新建一个资源。 - PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。 - PATCH(UPDATE):不建议用,因为不是HTTP标准请求,有兼容性问题。 - DELETE(DELETE):从服务器删除资源。 Http1.2的8种请求, OPTIONS HEAD GET POST PUT DELETE TRACE CONNECT ajax提交时,chrome浏览器测试结果,GET, POST, DELETE,PUT都支持, 在PUT时,后台无法获得参数,解决方法:在web.xml中配置过滤器 ~~~ <filter> <filter-name>HttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ~~~ Spring MVC控制器 ~~~ @Controller public class TestRestful { public List<UserInfo> users = new ArrayList<>(); public TestRestful() { UserInfo u1 = new UserInfo(); u1.setUserid(1); u1.setUsername("feiyy"); u1.setPassword("123"); users.add(u1); UserInfo u2 = new UserInfo(); u2.setUserid(2); u2.setUsername("xx"); u2.setPassword("123"); users.add(u2); UserInfo u3 = new UserInfo(); u3.setUserid(3); u3.setUsername("yy"); u3.setPassword("123"); users.add(u3); } @RequestMapping(value="/users", method=RequestMethod.GET) @ResponseBody public List<UserInfo> getAllUsers() { return users; } @RequestMapping(value="/users", method=RequestMethod.POST) @ResponseBody public List<UserInfo> addUser(UserInfo u) { users.add(u); return users; } @RequestMapping(value="/user/{id}", method=RequestMethod.PUT) @ResponseBody public List<UserInfo> modifyUser(@PathVariable int id, UserInfo u) { System.out.println(id); System.out.println(u.getUserid()); System.out.println(u.getUsername()); for(int i=0; i<users.size();i++) { UserInfo item = users.get(i); if(item.getUserid() == id) { item.setUsername(u.getUsername()); } } return users; } @RequestMapping(value="/user/{id}", method=RequestMethod.DELETE) @ResponseBody public List<UserInfo> deleteUser(@PathVariable int id) { for(int i=0; i<users.size();i++) { UserInfo item = users.get(i); if(item.getUserid() == id) { users.remove(item); break; } } return users; } @RequestMapping(value="/user/{id}", method=RequestMethod.GET) @ResponseBody public UserInfo getUserById(@PathVariable int id) { for(int i=0; i<users.size();i++) { UserInfo item = users.get(i); if(item.getUserid() == id) { return item; } } return null; } @RequestMapping(value="/user/{name}", method=RequestMethod.GET) @ResponseBody public UserInfo getUserByName(@PathVariable String name) { for(int i=0; i<users.size();i++) { UserInfo item = users.get(i); if(item.getUsername().equals(name)) { return item; } } return null; } @RequestMapping(value="/user/{name}/{pass}", method=RequestMethod.GET) @ResponseBody public UserInfo getUserByNamePass(@PathVariable String name, @PathVariable String pass) { for(int i=0; i<users.size();i++) { UserInfo item = users.get(i); if(item.getUsername().equals(name) && item.getPassword().equals(pass)) { return item; } } return null; } } ~~~ html页面: ~~~ $("#btn_getall").click(function(){ $.ajax({ url:"/TestSpringMVC/users", dataType:"json", success:function(data) { console.log(data); }, error:function(err) { console.log(err); } }); }); $("#btn_add").click(function(){ $.ajax({ url:"/TestSpringMVC/users", type:"post", data:{userid:"4",username:"eee",password:"eee"}, dataType:"json", success:function(data) { console.log(data); }, error:function(err) { console.log(err); } }); }); $("#btn_delete").click(function(){ $.ajax({ url:"/TestSpringMVC/user/4", type:"delete", dataType:"json", success:function(data) { console.log(data); }, error:function(err) { console.log(err); } }); }); $("#btn_update").click(function(){ $.ajax({ url:"/TestSpringMVC/user/1", type:"put", data:{userid:"1",username:"feiyuanyuan",password:"123"}, dataType:"json", success:function(data) { console.log(data); }, error:function(err) { console.log(err); } }); }); ~~~ 表单提交时,只支持get和post,解决方法: 1. 配置过滤器 ~~~ <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ~~~ 2. 表单加入隐藏属性_method ~~~ <form action="/TestSpringMVC/user/1" method="post"> <input type="hidden" name="userid" value="1"/> <input type="text" name="username"/> <input type="hidden" name="password" value="123"/> <input type="hidden" name="_method" value="PUT"/> <button type="submit">表单提交修改</button> </form> <form action="/TestSpringMVC/user/1" method="post"> <input type="hidden" name="userid" value="1"/> <input type="hidden" name="_method" value="DELETE"/> <button type="submit">表单提交删除</button> </form> ~~~